Skip to content

Keep killing an exec step's group until it is empty when mav dies - #177

Merged
bitomule merged 2 commits into
mainfrom
fix-exec-watchdog-fork-race
Dec 3, 2026
Merged

bitomule merged 2 commits into
mainfrom
fix-exec-watchdog-fork-race

Conversation

@bitomule

@bitomule bitomule commented Dec 3, 2026

Copy link
Copy Markdown
Owner

TestExecStepShellDoesNotSurviveMav failed now and then when several make check runs shared the machine. The test was right. The exec-step watchdog from #121 can really leave the step's shell running under launchd after mav run is SIGKILLed.

Cause

The watchdog read EOF on the death pipe and sent one kill -KILL -$$ to the step's process group, from inside that group. On macOS a signal sent to a whole process group can skip a member that is in the middle of fork(). A shell polling until [ -f … ]; do sleep 0.05; done forks twenty times a second, and so does a make starting compilers. When the skip hit, the watchdog's kill ended the watchdog itself, and nothing was left to send a second one.

Evidence, from instrumented runs of the test (a trace line from the watchdog plus ps/lsof of the group on failure):

  • In every failure the watchdog logged that it read EOF, sent the group SIGKILL and died from it. The shell was still alive with ppid 1, still forking sleep, and no other process held the pipe.

  • Same binary and the same load, run at the same time, 150 iterations each:

    • Shell forking in a loop: 7 survived. Same shell sitting in one sleep 1000, which never forks: 0 survived.
    • Killing the leader by pid before the group: 0 survived, against 16 with the group kill alone.

    So the group signal skips a process that is forking, and that isn't specific to the leader.

Fix

  • Exec steps (watchParentShell): the watchdog starts under set -m, so it gets a process group of its own. set +m switches job control off again before the step's command runs, so anything the command starts stays in the step's group as before. After EOF the watchdog repeats kill -KILL -$$ until kill fails, which only happens once the group is empty. The pgid can't be reused while a member still holds it, so the loop never signals a stranger.
  • On a normal step end, MAV closes its end of the pipe right after reapProcessGroup. The watchdog then backs up that single reap: on an empty group its first kill fails and it exits.
  • Background helpers: their watchdog already ran outside the group. It now repeats its final SIGKILL until the group is empty, for the same reason.
  • When TestExecStepShellDoesNotSurviveMav fails, its message now lists the group (ps), so the next failure shows who is still there.
  • New TestExecStepWatchdogLivesOutsideTheStepsGroup checks that the watchdog sits outside the step's group, which is what lets it retry, and that it is gone once a normal step ends, so the new design doesn't leak a process per step.

Validation

  • GOFLAGS=-count=1 make check, run at the same time in this checkout and in a git archive HEAD extract, 4 rounds each: 8 of 8 passed.

  • TestExecStepShellDoesNotSurviveMav looped with the same load on both sides, old binary vs. this branch, run at the same time:

    round old watchdog this branch
    1 1 / 150 0 / 300
    2 3 / 300 0 / 600
  • Ablation: with the old one-shot, in-group watchdog put back, TestExecStepWatchdogLivesOutsideTheStepsGroup fails with watchdog N is in the step's process group M; its first kill would end it before it could retry. With the fix it passes.

  • set -m / set +m checked with /bin/bash 3.2 both without a terminal and inside a pty, with the shell in a group of its own as Setpgid puts it. The watchdog gets its own pgid, the command's later background jobs stay in the shell's group, and nothing is printed or stopped.

A signal sent to a whole process group can skip a member that is in the
middle of fork(). The exec-step watchdog sent one SIGKILL to the group
from inside it, so a shell polling with sleep, or a make starting
compilers, could outlive a SIGKILLed mav with nothing left to stop it.

The watchdog now starts in a group of its own (set -m, then set +m
before the step's command) and repeats the SIGKILL until kill fails,
which only happens once the group is empty. The background helper
watchdog repeats its final SIGKILL the same way.
@bitomule
bitomule merged commit 451f36a into main Dec 3, 2026
1 check passed
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