feat(broker): end a confined worker's programs with it
Problem
A confined worker leads its own session, so the programs it starts share its process group. None of them ended with it:
- When the worker exited, a program it left running kept its
--directpaths, the runtime directories, the network, and the terminal. - When the broker got
SIGINT,SIGTERM, orSIGHUP, the worker died by its death signal. A program it had started has none, so it outlived both.
RFC 0010, accepted in #72, ends that group.
Change
When a confined worker exits, the broker:
- waits with
waitid(WNOWAIT), which leaves the worker an unreaped zombie - kills the group with
SIGKILL - reaps the worker and exits with its code
On SIGINT, SIGTERM, or SIGHUP, a thread waiting in sigwait kills the group. The main thread then reaps the worker, and the broker dies by that signal.
Details:
- When signals are blocked.
confine::commandblocks the three signals before any thread starts. A signal the broker inherited as ignored, as undernohup, is left ignored. - Thread order. The supervisor's thread starts before the worker, as the serve thread does.
- ID reuse. The two threads share the worker's ID behind a lock, only until just before it's reaped, so a late signal can't reach a reused ID.
- Before
setsid. Each kill also names the worker by its own ID, which covers the moment before it has calledsetsid. --unconfinedis unchanged. That worker shares the broker's process group, so killing the group would kill the broker and its caller.
A correction to the accepted RFC. It said std's Command resets the signal mask in children. Current std deliberately keeps the parent's mask, on both its fork/exec and posix_spawn paths, and exec keeps it too. Without a fix, a confined worker and everything it starts would run with those three signals blocked. So the confining process now unblocks them before it becomes the worker. The RFC text is corrected here.
unsafe. sigemptyset, sigaddset, sigaction, pthread_sigmask, sigwait, and raise go in the existing sys module of confine.rs, next to close_range, with the safety contract extended to cover them. Neither std nor rustix wraps them safely. waitid and the kills use rustix's safe wrappers.
Safety and compatibility
This is a behavior change for a confined worker, listed in the changelog under Changed. A worker that needs a program to outlive it must run with --unconfined. The documented limits are:
- a program that calls
setsidorsetpgid - anything after
SIGKILLto the broker
Verification
The commits went up in steps, so each CI run shows one thing. All of the new tests run the real fsgate-broker on Linux. A process is counted as dead when the pipe it holds reaches end of file. A 30-second timeout only turns a hang into a failure.
| Commit | What it has | Linux 6.6 result |
|---|---|---|
2c0fcdf | tests only | run: the program test and the stopped-broker test fail ("a program outlived its confined worker", "the worker or its program outlived a stopped broker"), and both controls pass. The signal test failed too, but only because the worker's program held the pipe; b67f96d gives it a worker with no program. |
5370df7 | the implementation, without the unblock step | run: the program and stopped-broker tests pass. Only a_confined_worker_can_still_be_stopped_by_a_signal fails, with "SIGTERM did not stop the confined worker", so it catches the blocked mask. |
9f81bad | the unblock step | run: all 8 checks passed. The five new tests passed on Linux 6.6 and 7.3, in all three runs of check, and in msrv. |
The tests:
a_program_a_confined_worker_leaves_running_ends_with_itstopping_the_broker_ends_a_confined_worker_and_its_programs:SIGTERM,SIGINT, andSIGHUP, with the broker's death by that signal checkeda_confined_worker_can_still_be_stopped_by_a_signal- controls:
a_program_that_leaves_the_group_outlives_its_worker(thesetsidlimit) andan_unconfined_worker_leaves_its_program_running
Locally on macOS:
cargo fmt --all --checkpassed.cargo clippy --all-targets -- -D warningspassed for the host, forx86_64-unknown-linux-gnu, and for that target withexperimental-intercept.cargo test --lockedpassed 106 tests and failed 0. The new tests are Linux-only, so none of them ran there.
Not verified:
- That a stop signal the broker inherited as ignored stays ignored. No test runs the broker under
nohup. - A signal that arrives before the worker has started.
Documentation and release
- The changelog gets a
### Changedentry. - RFC 0010 is corrected on the signal mask and gets the implementation notes.
- RFC 0008's step 2 and its "does not stop" list point to RFC 0010.
- The threat model gets three new rows and a narrowed limit row.
- The README's limit line is updated.
- The roadmap item is ticked, with evidence from the
9f81badrun.