fix: kill the agent's whole process group when ChildGuard drops#251
Open
SPIKESPIGEL404 wants to merge 1 commit into
Open
fix: kill the agent's whole process group when ChildGuard drops#251SPIKESPIGEL404 wants to merge 1 commit into
SPIKESPIGEL404 wants to merge 1 commit into
Conversation
AcpAgent's ChildGuard only killed the immediate child. Agents are commonly distributed behind wrapper launchers - the official agent registry's dominant distribution is npx/uvx - so the spawned tree is wrapper -> real agent. Killing the wrapper orphans the agent onto pid 1, where it does not reliably exit on stdin EOF and can outlive the client indefinitely (one leaked agent per session teardown). The child is now spawned as its own process-group leader (unix) and ChildGuard SIGKILLs the group before the direct-child kill fallback. This also covers the wrapper dying on its own while its grandchild lives. Windows keeps the existing single-kill behavior (Job Objects would be the equivalent; left as follow-up). Regression test spawns a bash->bash wrapper chain and asserts the grandchild dies when the guard drops. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
5 tasks
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.
Fixes #249.
Problem
ChildGuard::dropkills only the immediate child. The dominant agent-distribution pattern (per the official registry) is a wrapper launcher —npx → node,uvx → python— so teardown kills the wrapper and orphans the real agent onto pid 1, where it does not reliably exit on stdin EOF. One leaked agent process per session teardown, for everyAcpAgentconsumer. Observed in the wild with@zed-industries/claude-code-acplingering minutes after its client exited.Fix
spawn_processbuilds the command asstd::process::Commandand callsprocess_group(0)(unix) before converting withasync_process::Command::from, making the child its own process-group leader. Windows keeps the existingCREATE_NO_WINDOWpath untouched.ChildGuard::dropSIGKILLs the process group first (rustix::process::kill_process_group), then falls back to the existing direct-childkill()(a no-op double-tap on unix; the only behavior on non-unix). Group kill also covers the inverse failure — the wrapper dying on its own while its grandchild lives — because the group persists while any member does.rustixis added as a unix-only dependency; it is already in the tree transitively viaasync-process, so no new supply chain.Semantics note
Group-kill on drop means anything the agent's tree left running dies with the session. That is the natural reading of
ChildGuard's existing contract ("kills the process when dropped") extended to what the process is in practice — a tree. An agent intentionally daemonizing long-lived helpers would need to double-fork out of the group, which is the conventional unix opt-out.Not in this PR
Testing
test_child_guard_kills_wrapper_chain_process_group: spawns abash → bash inner.shchain (mimickingnpx → node;; :defeats bash's exec optimization so the chain really is two processes), captures the grandchild's pid, drops the guard, and asserts the grandchild is gone.claude-code-acp— zero surviving processes through a 4-levelenv → npx → npm → nodechain (feat(acp): per-session ACP substrate (acp serve|prompt) bitrouter/bitrouter#613).