fix(agent): reap agent process group on normal exit, not just cancellation#339
Open
Christopher-Li wants to merge 1 commit into
Open
Conversation
…ation ConfigureShellCommand isolates each agent/command in its own process group (Setpgid) and installs cmd.Cancel to SIGKILL the group, but cmd.Cancel only fires on context cancellation. The success path was never covered: when the test step's agent (or a configured test/lint command) exits 0, nothing kills the group, so grandchildren that outlived the leader - a vitest tinypool worker pool, a build watcher, a dev server - reparent to init and keep running. A clean vitest run leaked its worker pool every time, exhausting RAM. Add TerminateShellCommandGroup (unix SIGKILL of -pgid, windows best-effort taskkill /T, no-op elsewhere) and defer it after Start in every agent runner (claude, pi, acpx, codex) and the shell command path (runShellCommandWithEnv). The defer covers success, parse-error, and wait-error returns; cmd.Cancel still covers cancellation, where the defer is a harmless ESRCH no-op. Also install a 5s WaitDelay backstop on unix (mirroring windows) so a grandchild holding an inherited stdout/stderr pipe open can't wedge Wait forever. This fixes the general class, not just vitest: any agent-spawned grandchild leaking after a clean step is now reaped.
This was referenced Jun 29, 2026
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.
Problem
shellenv.ConfigureShellCommandisolates each agent/command in its own process group (Setpgid) and installscmd.Cancelto SIGKILL the group - butcmd.Cancelonly fires on context cancellation. The success path is never covered.When the test step's agent (or a configured test/lint command) exits 0, nothing kills the group. Grandchildren that outlived the leader - a vitest tinypool worker pool, a build watcher, a dev server - reparent to init (PPID 1) and keep running. A clean vitest run leaks its worker pool every time, which is the observed RAM-exhaustion (17×~1 GB orphan workers).
So cancellation was handled; clean completion was not.
Fix
shellenv.TerminateShellCommandGroupacross the build-tag split:SIGKILLof-pgid(the group persists while any member is alive, so it still reaps grandchildren that outlived the leader)taskkill /T /F /PIDdefer shellenv.TerminateShellCommandGroup(cmd)right afterStart()in every native agent runner (claude,pi,acpx,codex) and the shell-command path (runShellCommandWithEnv). The defer covers success, parse-error, and wait-error returns;cmd.Cancelstill covers cancellation, where the defer is a harmlessESRCHno-op.WaitDelaybackstop on unix (mirroring the existing windows backstop) so a grandchild holding an inherited stdout/stderr pipe open can't wedgeWaitforever.This fixes the general class, not just vitest: any agent-spawned grandchild leaking after a clean step is now reaped.
Tests
internal/shellenv:TerminateShellCommandGroupreaps a backgrounded grandchild left by a normally-exited leader; nil/never-started cmd is a no-op.internal/agent: drivesclaudeAgent.runOnceagainst a fake claude that backgrounds a grandchild and exits 0; asserts the grandchild is gone afterrunOncereturns. Confirmed this test fails without the defer (grandchild survives) and passes with it.Verification
gofmtclean ·make lint·go test -race ./...·make e2e· Windows + wasip1 cross-builds all pass.