-
Notifications
You must be signed in to change notification settings - Fork 103
Fix premature pytest debug session exit when multiprocessing child process terminates #1023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7c3ead
Initial plan
Copilot 3ea718c
Fix premature test session exit when child process debug session term…
Copilot 77d393e
Fix CI: bump minimum Python to 3.10 to fix pip 26.1+ incompatibility …
Copilot 06d6451
Update src/extension/debugger/hooks/childProcessAttachService.ts
rchiodo 8d2636d
Update src/test/pythonFiles/debugging/test_multiproc.py
rchiodo e9142db
Update src/test/unittest/hooks/childProcessAttachService.unit.test.ts
rchiodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| { | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "launch a file", | ||
| "type": "debugpy", | ||
| "request": "launch", | ||
| "program": "${file}", | ||
| "args": [], | ||
| "console": "integratedTerminal", | ||
| "justMyCode": false | ||
| }, | ||
| { | ||
| "name": "attach to a local port", | ||
| "type": "debugpy", | ||
| "request": "attach", | ||
| "port": 5678, | ||
| "host": "localhost", | ||
| "pathMappings": [ | ||
| { | ||
| "localRoot": "${workspaceFolder}", | ||
| "remoteRoot": "." | ||
| } | ||
| ], | ||
| "justMyCode": false | ||
| }, | ||
| { | ||
| "name": "attach to a local PID", | ||
| "type": "debugpy", | ||
| "request": "attach", | ||
| "processId": "${command:pickProcess}", | ||
| "justMyCode": false | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| Pytest test file demonstrating the multiprocessing issue when debugging. | ||
| When debugging pytest tests, creating a child process via multiprocessing.Process | ||
| and then waiting for it via process.join() can cause premature exit of the debug | ||
| session. Specifically, the debugger terminates immediately after the child process | ||
| terminates (i.e., at the point where process.join() returns), before reaching | ||
| subsequent statements in the test body. | ||
| This file contains tests that reproduce the issue described in: | ||
| https://github.com/microsoft/vscode-python-debugger/issues/548 | ||
|
rchiodo marked this conversation as resolved.
Outdated
|
||
| Workaround: Use a launch.json with "-s" (--capture=no) in pytest args, or | ||
| launch pytest via a custom launch.json with "module": "pytest". | ||
| """ | ||
| import multiprocessing as mp | ||
| import time | ||
|
|
||
|
|
||
| def _child_main() -> None: | ||
| """Simple child process that sleeps briefly then exits.""" | ||
| time.sleep(0.25) | ||
|
|
||
|
|
||
| def test_multiprocessing_join_in_test_body() -> None: | ||
| """ | ||
| Regression test: process.join() in a pytest test body should not cause | ||
| premature debugger exit. | ||
| When debugging this test, the debugger should reach print("after!") and | ||
| the assertion, not exit immediately after process.join() returns. | ||
| """ | ||
| 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 | ||
|
|
||
|
|
||
| def test_multiprocessing_nondaemon_join() -> None: | ||
| """ | ||
| Variant: non-daemon child process with join(). | ||
| Should behave the same as the daemon variant when debugging. | ||
| """ | ||
| proc = mp.Process( | ||
| target=_child_main, | ||
| name="debug_multiprocessing_nondaemon_child", | ||
| ) | ||
| proc.start() | ||
| proc.join(10) | ||
| assert proc.exitcode == 0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """ | ||
| Wait for a file to appear, then print 'done!' and exit. | ||
|
|
||
| This script is used to keep a debug session alive (by pausing the script | ||
| until the test harness is ready to terminate it), and to produce a known | ||
| output (useful for verifying that output was captured correctly). | ||
|
|
||
| Usage: | ||
| python wait_for_file.py <done_file> [output_file] | ||
| """ | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
|
|
||
| def wait_for_file(path: str) -> None: | ||
| while not os.path.exists(path): | ||
| time.sleep(0.1) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = sys.argv[1:] | ||
| if not args: | ||
| print("usage: wait_for_file.py <done_file> [output_file]", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| done_file = args[0] | ||
| output_file = args[1] if len(args) > 1 else None | ||
|
|
||
| wait_for_file(done_file) | ||
|
|
||
| if output_file: | ||
| with open(output_file, "w") as f: | ||
| f.write("done!\n") | ||
| else: | ||
| print("done!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.