Skip to content

Debugging pytest tests causes premature exit upon process.join() #981

Description

Behaviour

Debugging any pytest test that contains code that creates a child process via multiprocessing's mp.Process() and then later waits for that process to terminate via process.join() results in the debugger exiting early.

Specifically, the debugger terminates immediately after the child process terminates. It seems as though the debugger thinks the primary process has terminated, when in fact it's a child process.

Debugging the exact same code but as a standalone python file (i.e. not a test) doesn't cause this issue.

Steps to reproduce:

  1. Create a pytest file: (e.g. test_debugger.py)
import multiprocessing as mp
import time
def _child_main() -> None:
    time.sleep(0.25)

def test_multiprocessing_join_in_test_body() -> None:
    proc = mp.Process(
        target=_child_main,
        name="debug_multiprocessing_test_body_child",
    )
    proc.daemon = True
    proc.start()
    print("before!")
    proc.join(10)
    print("after!")
    assert proc.exitcode == 0
  1. Open the "Testing" tab on the Activity Bar
  2. Find the test and select "Debug Test"
  3. The test will exit right after proc.join(10) and be marked as skipped - i.e. it will not print "after!" and will not reach the assert

Diagnostic data

launch.json configuration

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug Current File (.venv)",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "python": "${workspaceFolder}\\.venv\\Scripts\\python.exe",
            "purpose": ["debug-test"],
            "console": "integratedTerminal",
            "justMyCode": false,
            "debugAdapterPath": "${workspaceFolder}\\.venv\\Lib\\site-packages\\debugpy\\adapter",
        }
    ]
}

Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)

2026-03-18 13:29:51.064 [info] arg: --rootdir already exists in args, not adding.
2026-03-18 13:29:51.064 [info] Attempting to use temp directory for test ids file, file name: test-ids-00c8f18e91ac982d37c4.txt
2026-03-18 13:29:51.066 [info] Environment variables set for pytest execution: PYTHONPATH=c:\Users\Seb\.vscode\extensions\ms-python.python-2026.4.0-win32-x64\python_files, TEST_RUN_PIPE=\\.\pipe\python-test-results-39db76ade74d9d04d426, RUN_TEST_IDS_PIPE=C:\Users\SEBAST~1\AppData\Local\Temp\test-ids-00c8f18e91ac982d37c4.txt
2026-03-18 13:29:51.066 [info] Running DEBUG pytest with arguments: --rootdir=c:\Users\Seb\Documents\_Repos\test_debug,--capture=no for workspace c:\Users\Seb\Documents\_Repos\test_debug 

2026-03-18 13:29:51.067 [info] Using configuration in launch.json
2026-03-18 13:29:59.546 [info] Test Result named pipe \\.\pipe\python-test-results-39db76ade74d9d04d426  cancelled

Output for Python Debugger in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python Debugger)

2026-03-18 13:29:51.452 [info] legacyGetInterpreterDetails: executable='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe' resource='c:\Users\Seb\Documents\_Repos\test_debug'
2026-03-18 13:29:51.452 [info] resolveAndUpdatePythonPath - Initial state: pythonPath='undefined', python='c:\Users\Seb\Documents\_Repos\test_debug\.venv\Scripts\python.exe', debugAdapterPython='C:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe', debugLauncherPython='C:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe', workspaceFolder='c:\Users\Seb\Documents\_Repos\test_debug'resolvedInterpreterPath='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe'
2026-03-18 13:29:51.487 [info] Resolving launch configuration with substituted variables
2026-03-18 13:29:51.503 [info] createDebugAdapterDescriptor: request='launch' name='Python: Debug Current File (.venv)'
2026-03-18 13:29:51.504 [info] legacyResolveEnvironment: Resolving environment 'C:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe'
2026-03-18 13:29:51.504 [info] legacyResolveEnvironment: Resolved executable='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe'
2026-03-18 13:29:51.504 [info] resolveEnvironment: legacy resolved executable='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe' version='3.14.2'
2026-03-18 13:29:51.504 [info] getExecutableCommand: executable='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe' version='3.14.2'
2026-03-18 13:29:51.505 [info] createDebugAdapterDescriptor: python command parts='c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe'
2026-03-18 13:29:51.505 [info] DAP Server launched with command: c:\Users\Seb\.pyenv\pyenv-win\versions\3.14.2\python.exe C:\Users\Seb\Documents\_Repos\test_debug\.venv\Lib\site-packages\debugpy\adapter
2026-03-18 13:29:52.180 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:52.184 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:52.186 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:54.283 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:58.802 [info] Resolving attach configuration with substituted variables
2026-03-18 13:29:58.802 [info] Using the only workspaceFolder found:  c:\Users\Seb\Documents\_Repos\test_debug
2026-03-18 13:29:58.808 [info] createDebugAdapterDescriptor: request='attach' name='Subprocess 34584'
2026-03-18 13:29:58.808 [info] Connecting to DAP Server at:  127.0.0.1:63314
2026-03-18 13:29:58.814 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:59.571 [info] Received 'debugpySockets' event from debugpy.
2026-03-18 13:29:59.596 [info] Received 'debugpySockets' event from debugpy.

debugpy.pydevd.34568.log
debugpy.adapter-19128.log
debugpy.pydevd.31448.log
debugpy.launcher-23484.log
debugpy.server-34568.log
debugger.vscode_5969cc3c-fa33-4f22-b0cd-4285a0e2cfe7.log
debugger.vscode_d64cf1b2-b9f0-47bf-9e42-418f78572c05.log

Activity

  1. SebMoore commented on Mar 18, 2026

    @SebMoore
    ContributorAuthor

    Not sure if this is an issue with vscode-python-debugger, vscode-python or even vscode-extension-test-runner, but putting it here for now :)

  2. rchiodo commented on Mar 18, 2026

    @rchiodo
    Contributor

    For me it just hangs. Not sure why yet.

  3. rchiodo commented on Mar 18, 2026

    @rchiodo
    Contributor

    Adding this to my launch.json lets me debug the test file as a workaround.

    {
        "name": "Debug pytest multiproc",
        "type": "debugpy",
        "request": "launch",
        "module": "pytest",
        "args": ["-s", "${workspaceFolder}/tests/test_multiproc.py"],
        "console": "integratedTerminal",
    }

    I get the following output with your test now:

    (base) (.venv312) PS C:\Users\rchiodo\source\repos\pylance-release\testing\single>  c:; cd 'c:\Users\rchiodo\source\repos\pylance-release\testing\single'; & 'c:\Users\rchiodo\source\repos\pylance-release\testing\single\.venv312\Scripts\python.exe' 'c:\Users\rchiodo\.vscode-insiders\extensions\ms-python.debugpy-2025.19.2026030601-win32-x64\bundled\libs\debugpy\launcher' '54437' '--' '-m' 'pytest' '-s' 'C:\Users\rchiodo\source\repos\pylance-release\testing\single/tests/test_multiproc.py' 
    =================================================================== test session starts ===================================================================
    platform win32 -- Python 3.12.10, pytest-9.0.2, pluggy-1.6.0
    rootdir: C:\Users\rchiodo\source\repos\pylance-release\testing\single
    collected 1 item                                                                                                                                           
    
    tests\test_multiproc.py before!
    after!
    .
    
    ==================================================================== 1 passed in 2.54s ==================================================================== 
    
  4. btc-nathand commented on May 6, 2026

    @btc-nathand

    I am seeing the same issue, and unfortunately the workaround Rich Chiodo (@rchiodo) suggested isn't working for me. I've had to just disable subprocess cleanup for debugging, and tolerate hanging processes. I do notice that the debugger call stack has the spawned processes on the same level as main, which seems to suggest that somewhere in the stack it's getting confused and treating these spawned processes as identical to the top-level (where exiting the process should close out the debug session)

  5. btc-nathand commented on May 11, 2026

    @btc-nathand

    I've installed the latest build (2026.7.11311009) with the changes on my VSC, and I'm still seeing the issue. I consolidated down to a minimal working example.

    1. create library.py
    from concurrent.futures import ProcessPoolExecutor, wait as futures_wait
    
    def worker_func():
            import random
            import time
            time_to_sleep = random.randint(5, 10)
            time.sleep(time_to_sleep)
            return time_to_sleep
    
    def library_function():
        print("doing some stuff...")
        futures = []
        with ProcessPoolExecutor(max_workers=8) as pool:
            for _ in range(16):
                fut = pool.submit(worker_func)
                futures.append(fut)
    
            done, _ = futures_wait(futures)
            for d in done:
                print(f"Done in {d.result()} seconds")
            print("Done with context manager")
    
        print("doing some more stuff...")
    
    1. create test.py
    from library import library_function
    
    def test_library():
        print("starting test")
        library_function()
        print("ending test")
    
    1. Populate minimal pytest.ini
    [pytest]
    testpaths = .
    pythonpath = .
    python_files = test*.py
    
    1. create minimal launch.json
    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug Test",
                "type": "debugpy",
                "purpose": ["debug-test"],
                "request": "launch",
                "console": "integratedTerminal",
                "justMyCode": false,
                "cwd": "${workspaceFolder}",
                "subProcess": true,
            },
        ]
    }
    

    Expected Behavior: expect to be able to reach all breakpoints
    Actual Behavior: the first breakpoints in library are reachable, but the final breakpoint (underlined in yellow), after the context manager shut-down, is never reached.

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

triage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions