Repository navigation
gh-160787: Reap the killed child in subprocess.run() on KeyboardInterrupt - #160941
Open
JakkulaVeerababu wants to merge 1 commit into
Open
JakkulaVeerababu wants to merge 1 commit into
JakkulaVeerababu wants to merge 1 commit into
Conversation
…dInterrupt On an exception, run() kills the child and relied on Popen.__exit__() to wait for it. But after a KeyboardInterrupt, __exit__() skips the wait once the SIGINT grace period has been used, so the killed child was never reaped and Popen.__del__() emitted a ResourceWarning. run() now waits for the killed child itself, with a timeout so that ^C never blocks indefinitely (bpo-25942). It calls _wait() directly like __exit__() does: the child was already killed, so wait()'s extra SIGINT grace period does not apply.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I found that subprocess.run() had a cleanup issue when Ctrl-C (KeyboardInterrupt) occurred. The child process was correctly killed, but it was not being reaped. run() relied on Popen.exit() for cleanup, but exit() intentionally skips waiting for the child during KeyboardInterrupt. As a result, Python could emit a misleading ResourceWarning saying the subprocess was still running even though it had already been terminated.
To fix this, I updated Lib/subprocess.py so that run() explicitly reaps the child process immediately after killing it. I used a bounded wait of up to 1 second to ensure the cleanup happens without violating CPython's requirement that Ctrl-C handling must not block indefinitely.
I also added a regression test, test_run_keyboardinterrupt_reaps_killed_child, in Lib/test/test_subprocess.py. The test simulates a KeyboardInterrupt during subprocess.run() and verifies that the child process has been collected by checking that it receives an exit status. The test fails before the fix and passes afterward.
Finally, I added a NEWS entry documenting the bug fix, while leaving the existing Ctrl-C tests unchanged to ensure no existing behavior was affected.
AI assistance
🤖 Generated with Claude Code
subprocess.run()can warn that a child is still running after killing it on Ctrl-C #160787