fix: reap cancelled subprocess commands after their parent exits - #2593
Open
zhang-bofan wants to merge 1 commit into
Open
zhang-bofan wants to merge 1 commit into
zhang-bofan wants to merge 1 commit into
Conversation
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.
When a command's parent exits while a descendant still holds stdout/stderr open, cancelling
SubprocessRuntime.run()leaves the descendant running:communicate()is unfinished, butproc.returncodeis already set and cleanup is skipped. A shell runningsleep 60 &reproduces this.Kill the command's process group when communication is interrupted, using the group ID established by
start_new_session=True, then re-raise the original exception. This also avoids looking up an exited parent withgetpgid().Validation on Linux / Python 3.11:
AGENTS.md.uv run --locked pytest tests/: 86 passed, 77 live-model tests skipped.uv run --locked pre-commit run --all-files: passed.uv run --locked --python 3.13 ty check verifiers: passed; the repository configuration excludesverifiers/v1from this check.Follow-up to #1628. The open #1995 handles pool shutdown and retains the foreground
returncodeguard; this fixes cancellation of an individual command while its worker remains alive.AI assistance: Codex was used to investigate, implement, and validate this change.
Note
Kill subprocess process group on exception in
SubprocessRuntime.runReplaces the prior conditional cleanup (which only killed the process group while the leader had no return code) with a
BaseExceptionhandler that always sendsSIGKILLto the process group identified by the subprocess PID before re-raising. This ensures descendants holding output pipes are reaped even after the leader exits. Normal successful completion no longer attempts a cleanup kill. Risk: anyBaseExceptionraised during subprocess communication now triggers a process-groupSIGKILL; verify that no callers of subprocess.py rely on child processes surviving an interrupted run.Macroscope summarized 79b9d51.