-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-83069: Use efficient event-driven subprocess.Popen.wait() on Linux / macOS / BSD
#144047
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
Open
giampaolo
wants to merge
66
commits into
python:main
Choose a base branch
from
giampaolo:subprocess-fast-wait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+301
−5
Open
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
288e4db
Use pidfd_open() and save the FD as an attribute
giampaolo 6e494ca
Create _busy_wait() and move code in there
giampaolo aaf193c
Move existing code in new _blocking_wait() method
giampaolo 7cbb0ad
Use _wait_pidfd()
giampaolo d9760c3
Use pidfd_open() not at class level but at method level
giampaolo 5fd8eec
Add kqueue() implementation for macOS and BSD
giampaolo 100b111
Add docstrings
giampaolo 74ac2f4
Be conservative and check for specific error codes
giampaolo fc0cfd6
Document possible failures
giampaolo f282459
Add missing import
giampaolo 6125976
Write test for pidfd_open() failing
giampaolo 41dc6c0
Write test case for pidfd_open() / kqueue failing. Assert fallback is…
giampaolo 73ba380
Write test case for kqueue failing. Assert fallback is used.
giampaolo 3c8e603
Move tests in their own class
giampaolo 932ae58
Add test for terminated PID race
giampaolo bb5080a
Add test_kqueue_race()
giampaolo f067073
Add test_kqueue_control_error
giampaolo 4d96c2f
Add docstring
giampaolo fe05acc
Guard against possible slow test
giampaolo adb444e
Timeout: use math.ceil to avoid truncation
giampaolo 4ec17c1
Remove unused exception var
giampaolo e807ba9
Timeout: use math.ceil to avoid truncation
giampaolo 1ddc52b
Replace _can_use_kqueue() -> _CAN_USE_KQUEUE
giampaolo 645ef6c
Shorten code
giampaolo 6ee771b
Use waitpid() + WNOHANG even if process exited to avoid rare race
giampaolo 61c6b99
Revert prev change
giampaolo 527646d
Use ceil(timeout) to avoid truncation
giampaolo 0a8a1b2
Remove check for timeout < 0 + rm test case which didn't make sense.
giampaolo 73b97dc
Don't use _blocking_wait() as it has a while loop
giampaolo 2d3c3f7
Add docstring
giampaolo 4eac42f
Rm _busy_wait()
giampaolo 3c156a9
Add comment
giampaolo dac7d3b
Add assert
giampaolo 5c7ec2f
Update comments
giampaolo 5c29144
Add test for timeout=0
giampaolo 4359b07
Update comment about PID reuse race
giampaolo 81275c8
Update comment
giampaolo 43b500f
Handle rare case where poll() says we're done, but waitpid() doesn't
giampaolo b64e42b
Update Doc/library/subprocess.rst
giampaolo a101406
Add news entry
giampaolo 452f8c4
Add entry in Doc/whatsnew/3.15.rst
giampaolo b0c9890
Fix typo
giampaolo 27b7c9f
Re-wording
giampaolo e1da996
Raise on timeout < 0 and re-add test case
giampaolo 5c78acc
Check if can really use can_use_pidfd() in unit tests
giampaolo df0538a
Check if can really use kqueue() in unit tests
giampaolo 6d8e36c
Pre-emptively check whether to use the fast way methods
giampaolo 6ba7465
Add test_fast_path_avoid_busy_loop
giampaolo e3c7977
Update comments
giampaolo 97cc3be
Fix missing import on Windows
giampaolo c4342e3
Merge branch 'main' into subprocess-fast-wait
giampaolo 5d78d24
Try to fix doc build error
giampaolo a916da3
Try to fix doc build error 2
giampaolo 86200bd
Try to fix doc build error 3
giampaolo 85c38bc
Try to fix doc build error 4
giampaolo 3c92c1d
Try to fix doc build error 5
giampaolo 33c8b1f
Minor rewordings
giampaolo 525c047
Small refact
giampaolo 2b931b8
Address review comments (bare assert, don't use ceil())
giampaolo d1c6e91
Address review comments (use test class VARIABLES)
giampaolo 3e9d303
Address review comments (add periods in doc)
giampaolo a44b2d7
Merge branch 'main' into subprocess-fast-wait
giampaolo b4f6020
Remove weird unicode char from doc added by accident
giampaolo 4e17856
Don't import select on Windows
giampaolo 6f6600d
Make kq.control() invocation clearer by passing kwargs
giampaolo 37288c6
Revert previous commit
giampaolo 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
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
7 changes: 7 additions & 0 deletions
7
Misc/NEWS.d/next/Library/2026-01-19-16-45-16.gh-issue-83069.0TaeH9.rst
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,7 @@ | ||
| :meth:`subprocess.Popen.wait`: when ``timeout`` is not ``None``, an efficient | ||
| event-driven mechanism now waits for process termination, if available. Linux | ||
| >= 5.3 uses :func:`os.pidfd_open` + :func:`select.poll`. macOS and other BSD | ||
| variants use :func:`select.kqueue` + ``KQ_FILTER_PROC`` + ``KQ_NOTE_EXIT``. | ||
| Windows keeps using ``WaitForSingleObject`` (unchanged). If none of these | ||
| mechanisms are available, the function falls back to the traditional busy loop | ||
| (non-blocking call and short sleeps). Patch by Giampaolo Rodola. |
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.