Repository navigation
exec drivers: a stop kills the command's whole process group - #118
Merged
Merged
Conversation
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
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
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.
The exec drivers' stop now reaches the whole command. This is the finding of #117.
The problem
CMDruns withsh -c. dash, theshof Debian and Ubuntu, runs the command as its child instead of becoming it. Sochild.kill()killed the shell alone, and the program ran on. A pipeline (a | b) kept both sides alive even withexec.The change
process::shell()starts the shell in a process group of its own, withCommandExt::process_group(0).process::Groupholds a started command.stop(), and a drop, send SIGKILL to the whole group, then reap the shell.waitidwithWNOWAITsees the shell end without reaping it.wait()uses it for a natural end, andended()for exec-rpc's check of its child.Group:Group::output(), which iswait_with_outputwith the group tracked.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
unsafe, butlibc::killpgwould need a new unsafe block.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.signalenablesprocess, which bringskillpgandwaitid.Tests
Unit tests (
connectors::process::tests):a_stop_kills_the_whole_pipelinestartssleep 30 | catwithshell()and sees its 3 processes (shell, sleep, cat) in the group, read from /proc. Afterstop(), and again after a drop, no process of the group is left.a_command_that_ends_keeps_its_code_and_its_outputchecks thatoutput()keeps the code, stdout and stderr, and thatended()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 withpgrep -g. Locally:Mutants:
left 2 processes of group …: sleep 739338551, cat), at SIGTERM and at SIGINT.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
exec.mdkeepsexecas a good habit, since it saves a shell that only waits, and says the stop no longer needs it.kill -9of the runtime still leaves its commands running; a process that leaves the group withsetsidis out of reach; a terminal's Ctrl-C reaches the runtime alone.execclause.Left out
🤖 Generated with Claude Code
https://claude.ai/code/session_018wXgxSSYsPm88PCqu98HPT