Skip to content

exec drivers: a stop kills the command's whole process group - #118

Merged
cpoder merged 2 commits into
masterfrom
fix/exec-process-group
Dec 5, 2026
Merged

cpoder merged 2 commits into
masterfrom
fix/exec-process-group

Conversation

@cpoder

@cpoder cpoder commented Dec 5, 2026

Copy link
Copy Markdown
Owner

The exec drivers' stop now reaches the whole command. This is the finding of #117.

The problem

CMD runs with sh -c. dash, the sh of Debian and Ubuntu, runs the command as its child instead of becoming it. So child.kill() killed the shell alone, and the program ran on. A pipeline (a | b) kept both sides alive even with exec.

The change

  • process::shell() starts the shell in a process group of its own, with CommandExt::process_group(0).
  • A new process::Group holds a started command.
    • stop(), and a drop, send SIGKILL to the whole group, then reap the shell.
    • The shell stays unreaped until its id has left the set of running groups, so a kill can never reach an id that another process has taken.
    • waitid with WNOWAIT sees the shell end without reaping it. wait() uses it for a natural end, and ended() for exec-rpc's check of its child.
  • Every stop goes through Group:
    • exec-stream-source and exec-rpc when their unit stops (a reload, the runtime's stop);
    • exec-rpc's probe;
    • every error return of exec-rpc, through the drop.
  • exec-source and exec-sink wait on their command through Group::output(), which is wait_with_output with the group tracked.
  • The runtime's stop calls connectors::stop_commands() after the units' 800 ms. It kills every group still running, including a command that an exec-source or an exec-sink waits on, and starts no new one. A group no longer gets a terminal's Ctrl-C, so this keeps the runtime's stop complete.

The crate: nix

  • core does not forbid unsafe, but libc::killpg would need a new unsafe block.
  • nix 0.31.3 is already in the lock, under ctrlc, with default-features = false, features = ["signal"] on unix. core asks for exactly that, so there is no new crate and no new feature to compile. The lock change is one line, "nix" in core's dependency list.
  • signal enables process, which brings killpg and waitid.
  • rustix is not in the tree, so it would be a new crate.

Tests

  • Unit tests (connectors::process::tests):

    • a_stop_kills_the_whole_pipeline starts sleep 30 | cat with shell() and sees its 3 processes (shell, sleep, cat) in the group, read from /proc. After stop(), and again after a drop, no process of the group is left.
    • a_command_that_ends_keeps_its_code_and_its_output checks that output() keeps the code, stdout and stderr, and that ended() sees an exit without reaping.
  • e2e/exec-stop.sh, which is new, runs in the admission job. It uses its own ports. Each command carries a marker made from the script's PID, which finds its group, and the group is then counted by its id with pgrep -g. Locally:

    == a reload stops the unit, and its whole pipeline
      ok: the pipeline runs as 3 processes in group 3942890: the shell, sleep and cat
      ok: a reload: no process of group 3942890 left
    == the runtime stops on SIGTERM, and every command of its exec units with it
      ok: SIGTERM: no process left of the pipeline (group 3942929) nor of the command waited on (group 3942942)
    == the runtime stops on SIGINT, and every command of its exec units with it
      ok: SIGINT: no process left of the pipeline (group 3942986) nor of the command waited on (group 3942999)
    
  • Mutants:

    • Kill the child alone, as before. The unit test fails, and the e2e fails at the reload (left 2 processes of group …: sleep 739338551, cat), at SIGTERM and at SIGINT.
    • Drop the stop_commands() call from the runtime's stop. The e2e fails at SIGTERM and SIGINT: the command that exec-source waits on is left with its 3 processes. The pipeline is still stopped by its own driver.
  • e2e/offset-resume.sh (kill -9 in the middle of a stream, then resume) passes on the new build.

Docs

Left out

  • A command that an exec-source or an exec-sink waits on is not killed when its unit stops by a reload. It finishes as before; the runtime's stop is what kills it.
  • The kcat SCRAM failure belongs to step 4.

🤖 Generated with Claude Code

https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT

CMD runs with sh -c, and dash runs the command as its child: child.kill()
killed the shell alone, and the program, both sides of a pipeline among
them, ran on. shell() now starts the shell in a process group of its own
(CommandExt::process_group(0)), and a Group stops it whole: SIGKILL to the
group with nix's killpg, then the shell reaped. The shell stays unreaped
until it is out of the set of running groups, so a kill never reaches an
id another process took. waitid with WNOWAIT sees a shell end without
reaping it, for a natural end and for exec-rpc's check of its child.

Every stop goes through it: exec-stream-source and exec-rpc when their
unit stops, exec-rpc's probe, and an error return of exec-rpc, by the
drop. exec-source and exec-sink wait on their command through it, so the
runtime's stop, after the units' 800 ms, kills what still runs, a command
waited on included: a group is out of reach of a terminal's Ctrl-C.

nix 0.31 with its signal feature is in the tree already, under ctrlc, with
that one feature: no new crate and no unsafe block of ours.
e2e/exec-stop.sh runs a pipeline without exec under exec-stream-source and
a command exec-source waits on, and finds no process of their groups after
a reload, a SIGTERM and a SIGINT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
@cpoder
cpoder marked this pull request as ready for review December 5, 2026 08:07
It took the set, let the lock go, then killed: a shell reaped in between
freed its id, which a new process could take before the kill reached it.
The lock now stays held through the kills, and a reap waits for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT
@cpoder
cpoder merged commit 16164e0 into master Dec 5, 2026
7 checks passed
@cpoder
cpoder deleted the fix/exec-process-group branch December 5, 2026 08:31
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