Skip to content

Stop an exec step's shell from outliving the mav that started it - #121

Merged
bitomule merged 1 commit into
mainfrom
fix/exec-kills-process-group
Sep 21, 2026
Merged

bitomule merged 1 commit into
mainfrom
fix/exec-kills-process-group

Conversation

@bitomule

Copy link
Copy Markdown
Owner

What was wrong

An exec step's timeout bounds the step, not the shell. The timeout, the
Cancel that kills the process group, and the WaitDelay all run inside mav, so
kill -9 on a mav run leaves the shell it had mid-step reparented to launchd,
running until the machine is rebooted. A barrier like
until [ -f "$sentinel" ]; do sleep 0.05; done — what concurrent_run_test.go
uses to interleave two agents, and what anyone waiting on a build would write —
then polls at 20Hz forever, for a file under a t.TempDir() that no longer
exists.

Measured on the reporting machine: 348 such shells, load average 124 with no
process above 15% CPU, and a fresh batch from every go test ./....

The fix

The shell carries its own death signal. mav holds the write end of a pipe and
never writes to it; the shell gets the read end as fd 3 (ExtraFiles) and
backgrounds a read on it, which returns EOF the moment the kernel closes mav's
descriptors — on any exit, SIGKILL included. The shell then SIGKILLs its own
process group, which Setpgid had already made its own alone.

Not kill -0 $PPID polling, which was the first shape of this fix and does not
hold: kill -0 succeeds on a zombie, and a parent SIGKILLed by a harness
that never reaps it (a test's t.Cleanup does exactly that) stays a zombie for
as long as the harness lives.

Ablation

Same test, three builds:

build TestExecStepShellDoesNotSurviveMav
no fix FAIL — shell outlives mav, still polling
kill -0 $PPID watchdog FAIL — parent is a zombie and still answers
pipe/EOF watchdog (this PR) PASS in 0.3s

Process counts around TestConcurrentRunsKeepSeparateWorkers, killing both mav
children mid-step exactly as the test's own cleanup does:

build orphan shells before after
no fix 17 18
this PR 17 17

And instrumented across a whole go test ./...: the pid-polling version started
1554 watchdogs and fired 98, leaving 205 orphans. With this version, a full
go test ./... ended with 0 orphan shells.

TestConcurrentRunsKeepSeparateWorkers still proves what it proved — both agents
reach their barriers (counted live during the run) before either is killed.

Audit of the rest of the family

  • ExecRunner.Start survivors (probe-logs, video, mitmdump, the run worker):
    intentional, and every one is recorded in processes.jsonl and collected by
    mav stop or the worker's lease-expiry reapAbandonedRun — the doctrine
    established in Stop mav open --no-relaunch stacking a log stream per call #119. Not the same hole.
  • target_command and launch recipes (/bin/bash -lc and /bin/sh -lc
    through Runner.Run): the ctx deadline kills the direct shell while mav lives,
    but anything they background survives, and so does the shell itself if mav is
    SIGKILLed. Same family, smaller blast radius; left out of this PR because
    closing it means giving the shared Runner.Run a process group of its own, and
    target.go documents that launch recipes legitimately background a helper.
  • The run worker itself does outlive its parent by design, bounded only by
    the 15-minute idle lease. Reported separately; not touched here.

An exec step's timeout bounds the step, not the shell. The timeout, the
Cancel that kills the process group and the WaitDelay all run inside mav,
so `kill -9` on a `mav run` leaves the shell it had mid-step reparented to
launchd, running for as long as the machine is up. A barrier like
`until [ -f "$sentinel" ]; do sleep 0.05; done` -- what
concurrent_run_test.go uses to interleave two agents, and what anyone
waiting on a build would write -- then polls at 20Hz forever, for a file
under a t.TempDir() that no longer exists. Measured on one machine: 348 of
them, load average 124 with nothing above 15% CPU, and a fresh batch from
every `go test ./...`.

The shell now carries its own death signal. mav holds the write end of a
pipe and never writes to it; the shell gets the read end as fd 3 and
backgrounds a `read` on it, which returns EOF the moment the kernel closes
mav's descriptors -- on any exit, SIGKILL included. The shell then SIGKILLs
its own process group, which Setpgid had already made its own alone.

Not `kill -0 $PPID` polling, which was the first shape of this fix and does
not hold: `kill -0` succeeds on a zombie, and a parent SIGKILLed by a
harness that never reaps it stays a zombie as long as that harness lives --
exactly what a test's t.Cleanup does. With that version in, one
`go test ./...` started 1554 watchdogs and fired 98, and left 205 shells
polling a dead parent that still answered.

TestExecStepShellDoesNotSurviveMav covers it and kills the mav child
without reaping it on purpose, so the zombie is the case under test.

Claude-Session: https://claude.ai/code/session_018Qmew9oEQ6frPLSrE8XUm5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant