instances: stop with KillMode=control-group under PrivatePIDs (#528)
systemd 257 sometimes marks a PrivatePIDs unit's main process 'not our child'. A KillMode=mixed stop then has nothing to wait on after its SIGTERM and SIGKILLs at once (Result=signal, no drain). control-group signals the tenant too, so the stop waits as designed. Spec, plan and an invariant test over every PrivatePIDs unit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
19 files changed,
+349
−41
docs/superpowers/plans/2026-11-08-instance-stop-privatepids-528.md+43 −0docs/superpowers/specs/2026-11-08-instance-stop-privatepids-528-design.md+146 −0internal/activator/genbackend.go+2 −2internal/host/claimunit_template.go+1 −1internal/host/units/carlos-instance-cmd@.service+8 −2internal/host/units/carlos-instance-gen@.service+8 −1internal/host/units/carlos-instance-rootfs-cmd@.service+8 −2internal/host/units/carlos-instance-rootfs-gen@.service+8 −1internal/host/units/carlos-instance-rootfs-tty@.service+8 −2internal/host/units/carlos-instance-rootfs-worker@.service+8 −3internal/host/units/carlos-instance-rootfs@.service+8 −3internal/host/units/carlos-instance-seed@.service+8 −3internal/host/units/carlos-instance-sidecar@.service+8 −3internal/host/units/carlos-instance-tty@.service+8 −2internal/host/units/carlos-instance@.service+8 −3internal/host/units_isolation_test.go+6 −6internal/host/units_privatepids_test.go+54 −0internal/host/units_rootfs_test.go+1 −1internal/instanceexec/init_linux.go+8 −6
diff --git a/docs/superpowers/plans/2026-11-08-instance-stop-privatepids-528.md b/docs/superpowers/plans/2026-11-08-instance-stop-privatepids-528.md| new file mode 100644 |
| index 000000000..c8f6e93cd |
| --- /dev/null |
| +++ b/docs/superpowers/plans/2026-11-08-instance-stop-privatepids-528.md |
| @@ -0,0 +1,43 @@ |
| +# Plan: instance stops under PrivatePIDs (#528) |
| + |
| +Spec: `docs/superpowers/specs/2026-11-08-instance-stop-privatepids-528-design.md`. |
| +Branch `instance-stop-528` from origin/main 1751f78f. |
| + |
| +## Task 1: invariant test (red) |
| + |
| +`internal/host/units_isolation_test.go`: add |
| +`TestPrivatePIDsUnitsKillControlGroup`. For every embedded `units/*.service` |
| +and `claimBoundTemplate` that has `PrivatePIDs=yes`, refuse any `KillMode=` |
| +line other than `KillMode=control-group`. Move the |
| +existing pins (`units_isolation_test.go` per-template table and the cmd |
| +keep-list, `units_rootfs_test.go` common list) from `mixed` to |
| +`control-group`. Run with `-run`; expect failures naming the eleven |
| +tenant templates and the claim-bound one. |
| + |
| +## Task 2: templates (green) |
| + |
| +Set `KillMode=control-group` in the eleven tenant templates and in |
| +`claimunit_template.go`. Rewrite each template's stop comment: SIGTERM to |
| +every process in the unit, SIGKILL after `TimeoutStopSec`, and why `mixed` |
| +is wrong under PrivatePIDs (#528). Update the trampoline's "Forward" comment |
| +in `internal/instanceexec/init_linux.go` and the `KillMode=mixed` mention in |
| +`internal/activator/genbackend.go`. Re-run Task 1's tests plus the |
| +`internal/host` unit-template tests (`-run 'Unit|Template|Rootfs|PrivatePIDs'`). |
| + |
| +## Task 3: root-runner witness |
| + |
| +`internal/host/instancestop_rootrunner_linux_test.go` |
| +(`//go:build linux && rootrunner`): `TestRootInstanceStopUnderPrivatePIDs` |
| +as specified, with the tenant dispatched from the rootrunner `TestMain` |
| +(`instance-stop-tenant`). It installs this binary at `/opt/carlos/carlos` |
| +only on a fresh runner, refusing otherwise. Cross-compile it locally |
| +(`GOOS=linux GOARCH=arm64 go test -c -tags rootrunner`), then run it on the |
| +root runner with `root-runner.py` (sandbox off) and confirm no leaked |
| +`purpose=plan14-root-runner` instance afterwards. |
| + |
| +## Task 4: review, census, land |
| + |
| +Astra review (max 3 rounds; stop at the first round with no P1). Census only |
| +if CI's `TestPlan14RootReadCensus` fails. Push, describe, CI green on the |
| +exact tip, `amadan branch merge carlos/platform instance-stop-528 -squash |
| +-expect <sha>`. Reply on #528 with the root cause and the merged SHA. |
diff --git a/docs/superpowers/specs/2026-11-08-instance-stop-privatepids-528-design.md b/docs/superpowers/specs/2026-11-08-instance-stop-privatepids-528-design.md| new file mode 100644 |
| index 000000000..859a0b375 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-11-08-instance-stop-privatepids-528-design.md |
| @@ -0,0 +1,146 @@ |
| +# Instance stops under PrivatePIDs (#528) |
| + |
| +Date: 2026-11-08. Status: design for the start-hardening brief (Fix 2). |
| + |
| +## Problem |
| + |
| +Some instance unit stops end `Result=signal`: the app is SIGKILLed with no |
| +SIGTERM grace. The demo kernel witness (`TestDemoDenyAllKernel`) requires |
| +`Result=success` at stop and fails on it. |
| + |
| +## Evidence (dev, read-only, 2026-11-08) |
| + |
| +- Demo box (i-05789ae978ebf2608): 17 of 49 `carlos-instance@` stops failed |
| + with `signal`. Each failed stop has the same journal shape, for example the |
| + hibernation of serene-orchid-33 at 2026-11-05 13:54:56: |
| + |
| + Killing process 59525 (carlos) with signal SIGKILL. |
| + Stopping carlos-instance@serene\x2dorchid\x2d33... |
| + Main process exited, code=killed, status=9/KILL |
| + Failed with result 'signal'. |
| + |
| + The stop was a plain `systemctl stop` from instance-control (the |
| + hibernation's `Control.Stop`). SIGKILL is the first signal systemd logged, |
| + 24 ms after the activator logged "hibernating", and the main process was |
| + dead 3 ms later. (systemd logs a SIGTERM only when it is "noteworthy", so |
| + the SIGTERM it sent first does not appear.) |
| +- Matching each stop result to its invocation's own "not our child" line |
| + (`Supervising process N which is not our child`, journal field |
| + `INVOCATION_ID`) separates the two cases completely: |
| + |
| + | box | units | main not alien | main alien | |
| + | --- | --- | --- | --- | |
| + | demo box | `carlos-instance@` | 27 ok, 0 failed | 5 ok, 17 failed `signal` | |
| + | sophie (dev-ie) | `carlos-instance-gen@` | 259 ok, 0 failed | 11 ok, 73 failed `signal` | |
| + |
| + No stop failed with `signal` unless its main process was alien. The few |
| + alien invocations that ended "ok" were not checked one by one; an |
| + invocation whose main process exits before any stop never reaches the |
| + kill path. |
| + The oneshot probes are alien in 10 to 27% of starts and always end ok, |
| + because nobody stops them. |
| + |
| +## Root cause |
| + |
| +systemd 257, read in the v257.9 source: |
| + |
| +1. With `PrivatePIDs=yes`, `setup_private_pids()` (exec-invoke.c) forks |
| + once more after `unshare(CLONE_NEWPID)`. The parent sends the child's |
| + pidref to the manager and `_exit()`s. The child, PID 1 of the new |
| + namespace, becomes the main process. |
| +2. The manager takes that pidref in its event loop and calls |
| + `service_set_main_pid()`, which checks `pidref_is_my_child()`. If the |
| + forking parent has not exited and been reaped yet, the child's parent is |
| + still that intermediate process. systemd then marks the main PID |
| + **alien** (`main_pid_alien = true`) and logs "not our child". This is a |
| + race, lost on roughly 10 to 45% of starts on our boxes. The flag stays |
| + set for the life of the invocation. Upstream `main` still has this code |
| + (the `FIXME: Supervise through pidfd here`). |
| +3. At stop, `service_enter_signal(SERVICE_STOP_SIGTERM)` calls |
| + `unit_kill_context()`. For the main process, `unit_kill_context_one()` |
| + sends SIGTERM but returns `!is_alien`, so 0 for an alien main. Under |
| + `KillMode=mixed` the SIGTERM phase does not touch the rest of the cgroup. |
| + Nothing reports "worth waiting for", so `wait_for_exit` is false. |
| +4. With `SendSIGKILL=yes` (the default), `service_enter_signal()` then goes |
| + straight to `SERVICE_STOP_SIGKILL`. The SIGTERM was sent, but the |
| + SIGKILL follows within microseconds. The trampoline never gets to |
| + forward the SIGTERM, the app gets no drain, and the main process dies |
| + by signal 9, which `SuccessExitStatus=` cannot excuse. |
| + |
| +`PrivatePIDs=yes` is not wrong; it is half of the race. The other half is |
| +`KillMode=mixed`, under which an alien main process is the only thing the |
| +SIGTERM phase can wait on. |
| + |
| +## Fix |
| + |
| +Use `KillMode=control-group` in every unit the platform writes with |
| +`PrivatePIDs=yes`, so no instance template uses `mixed` with PrivatePIDs. |
| +That covers eleven tenant templates (run, gen, seed, sidecar, cmd and tty, plus |
| +rootfs run, gen, worker, cmd and tty) and the claim-bound template in |
| +`claimunit_template.go`. The demo build and snapshot units already use |
| +`control-group`. The probes are oneshots that exit on their own. |
| + |
| +Under `control-group` the SIGTERM phase also signals every other process in |
| +the unit's cgroup. A running instance always has one, the app under the |
| +trampoline, so `cg_kill_recursive()` reports a kill and systemd arms the |
| +`TimeoutStopSec=20` timer and waits, however the main-PID race went. The |
| +stop then ends as it was designed to: |
| + |
| +- the app drains on SIGTERM and exits; |
| +- the trampoline exits with the app's status, or 128+15 = 143, which |
| + `SuccessExitStatus=129 130 141 143` counts as clean; |
| +- anything still running after 20 s gets SIGKILL from systemd, as before. |
| + |
| +Nothing about isolation changes: same namespaces, same uid, same filters, |
| +same cgroup and the same bound on stop time. |
| + |
| +### What tenants see differently |
| + |
| +- **The app's own children get SIGTERM at stop**, as under systemd's default |
| + `KillMode`, instead of SIGKILL once the app has exited. The caps renderer |
| + already gives "children" apps `KillMode=control-group` for the same |
| + reason. |
| +- **The app can see SIGTERM twice**: once from systemd directly and once |
| + forwarded by the trampoline. Both arrive within microseconds, and a |
| + standard signal still pending when the second arrives is merged with the |
| + first. Handlers in common runtimes are idempotent (Go's `signal.Notify` |
| + drops into a full channel, Node's server close is a no-op the second time, |
| + Python's and PHP's default action is to exit). The trampoline keeps |
| + forwarding because some SIGTERMs reach only the main process, for example |
| + a claims-on generation's `Retire`, which signals the unit's main PID. |
| + |
| +### Demos never outlive their time |
| + |
| +Demo takedown and expiry stop units through instance-control |
| +(`stopDemoUnits`: `systemctl stop` on each unit, then on the slice). The stop |
| +is bounded the same way as before: SIGTERM, at most `TimeoutStopSec=20`, then |
| +SIGKILL to the whole cgroup. Previously an alien demo was SIGKILLed at once; |
| +now it gets the same grace as every other app and is still killed if it |
| +ignores SIGTERM. The witness proves this bound with an app that ignores |
| +SIGTERM. |
| + |
| +## Tests |
| + |
| +- **Invariant (unit tests, CI):** no embedded unit and not the claim-bound |
| + template may carry a `KillMode=` other than `control-group` beside |
| + `PrivatePIDs=yes`. A unit with no `KillMode=` line gets systemd's |
| + default, `control-group`; the probes rely on that. The existing |
| + per-template pins move from `mixed` to `control-group`. |
| +- **Real-box witness (root runner, `rootrunner` tag):** |
| + `TestRootInstanceStopUnderPrivatePIDs`. It installs the embedded |
| + `carlos-instance@.service`, with only its `[Unit]` couplings removed and a |
| + test name, so every `[Service]` line ships as written. The real trampoline |
| + runs, because the rootrunner `TestMain` dispatches `instance-exec` and |
| + `instance-exit`. The test: |
| + 1. runs the same start-and-stop cycle many times with `KillMode=mixed` |
| + restored by drop-in, and requires at least one `Result=signal` with a |
| + "not our child" line, which proves the race reproduces on the runner; |
| + 2. runs the cycle as many times on the shipped template, and requires |
| + `Result=success` every time and that the app saw SIGTERM; |
| + 3. stops an app that ignores SIGTERM, with `TimeoutStopSec=` shortened by |
| + drop-in, and requires that it is killed within the bound and that its |
| + cgroup is empty. |
| + |
| +## Out of scope (recorded on #528) |
| + |
| +- Reporting the alien-main race upstream to systemd. |
diff --git a/internal/activator/genbackend.go b/internal/activator/genbackend.go| index 2d05c8399..576fdb579 100644 |
| --- a/internal/activator/genbackend.go |
| +++ b/internal/activator/genbackend.go |
| @@ -505,8 +505,8 @@ func (b *SplitBackend) StartSidecarGen(ctx context.Context, rt registry.Route, g |
| // returns once its cgroup is empty (zero-downtime spec §7.4, §7.5): the |
| // leader's exit says nothing of a process it forked. Flat with containment: |
| // the role-sidecar child (plan 2 D4). Nested: the sidecar unit's stop, which |
| -// returns once systemd has emptied its cgroup (KillMode=mixed sends SIGKILL |
| -// to whatever SIGTERM left). Without containment the sidecar shares the |
| +// returns once systemd has emptied its cgroup (KillMode=control-group sends |
| +// SIGKILL to whatever SIGTERM left). Without containment the sidecar shares the |
| // host cgroup with the instance, and the terminal stop's evidence on that |
| // cgroup is the proof (errNoGenContainment, as KillGen answers). |
| // |
diff --git a/internal/host/claimunit_template.go b/internal/host/claimunit_template.go| index 62de03963..8fdfa3e38 100644 |
| --- a/internal/host/claimunit_template.go |
| +++ b/internal/host/claimunit_template.go |
| @@ -13,7 +13,7 @@ User=carlos |
| Group=carlos |
| Slice=carlos-instances-%i.slice |
| Restart=no |
| -KillMode=mixed |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| SuccessExitStatus=129 130 141 143 |
| StandardOutput=file:/run/carlos/logs/%I.run.sock |
diff --git a/internal/host/units/carlos-instance-cmd@.service b/internal/host/units/carlos-instance-cmd@.service| index 040660163..d9b391765 100644 |
| --- a/internal/host/units/carlos-instance-cmd@.service |
| +++ b/internal/host/units/carlos-instance-cmd@.service |
| @@ -28,8 +28,14 @@ ExecStopPost=/opt/carlos/carlos instance-exit cmd %I |
| User=carlos |
| Group=carlos |
| Slice=carlos-instances-%i.slice |
| -# SIGTERM to the command, SIGKILL to anything left 20 s later. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # No SuccessExitStatus=: the agent reads the exit status itself. |
| # Two listeners, so stdout and stderr stay apart (run spec §5.5). PID 1 |
diff --git a/internal/host/units/carlos-instance-gen@.service b/internal/host/units/carlos-instance-gen@.service| index a0b3a263e..bf190ebdc 100644 |
| --- a/internal/host/units/carlos-instance-gen@.service |
| +++ b/internal/host/units/carlos-instance-gen@.service |
| @@ -31,7 +31,14 @@ User=carlos |
| Group=carlos |
| # The activator owns recycling; systemd must not race it. |
| Restart=no |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the app as its child (#126): it forwards the SIGTERM and exits |
diff --git a/internal/host/units/carlos-instance-rootfs-cmd@.service b/internal/host/units/carlos-instance-rootfs-cmd@.service| index a85ff7570..7ec00671f 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-cmd@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-cmd@.service |
| @@ -31,8 +31,14 @@ ExecStopPost=/opt/carlos/carlos instance-exit --rootfs cmd %I |
| User=carlos |
| Group=carlos |
| Slice=carlos-instances-%i.slice |
| -# SIGTERM to the command, SIGKILL to anything left 20 s later. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # No SuccessExitStatus=: the agent reads the exit status itself. |
| # Two listeners, so stdout and stderr stay apart (run spec §5.5). PID 1 |
diff --git a/internal/host/units/carlos-instance-rootfs-gen@.service b/internal/host/units/carlos-instance-rootfs-gen@.service| index 540b519fe..012ff26c0 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-gen@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-gen@.service |
| @@ -35,7 +35,14 @@ User=carlos |
| Group=carlos |
| # The activator owns recycling; systemd must not race it. |
| Restart=no |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the app as its child (#126): it forwards the SIGTERM and exits |
diff --git a/internal/host/units/carlos-instance-rootfs-tty@.service b/internal/host/units/carlos-instance-rootfs-tty@.service| index f2d048cd3..9e1ca042b 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-tty@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-tty@.service |
| @@ -30,8 +30,14 @@ ExecStopPost=/opt/carlos/carlos instance-exit --rootfs tty %I |
| User=carlos |
| Group=carlos |
| Slice=carlos-instances-%i.slice |
| -# SIGTERM to the session, SIGKILL to anything left 20 s later. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # No SuccessExitStatus=: the agent reads the exit status itself. |
| # One socket, the agent's: PID 1 connects stdin to it as root, outside the |
diff --git a/internal/host/units/carlos-instance-rootfs-worker@.service b/internal/host/units/carlos-instance-rootfs-worker@.service| index 270142e6a..9986746a9 100644 |
| --- a/internal/host/units/carlos-instance-rootfs-worker@.service |
| +++ b/internal/host/units/carlos-instance-rootfs-worker@.service |
| @@ -45,9 +45,14 @@ Group=carlos |
| # The activator owns recycling: a crash stands the worker down and the |
| # instance's next wake starts it again. systemd must not race that. |
| Restart=no |
| -# SIGTERM to the worker, SIGKILL to anything it left behind after |
| -# TimeoutStopSec; the activator's stop budget is 20s. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the worker as its child (#126): it forwards the SIGTERM and |
diff --git a/internal/host/units/carlos-instance-rootfs@.service b/internal/host/units/carlos-instance-rootfs@.service| index 3cb29f5b0..1ca2cac21 100644 |
| --- a/internal/host/units/carlos-instance-rootfs@.service |
| +++ b/internal/host/units/carlos-instance-rootfs@.service |
| @@ -41,9 +41,14 @@ Slice=carlos-instances-%i.slice |
| # The activator owns recycling: a crash stands the entry down and the next |
| # request wakes it. systemd must not race that. |
| Restart=no |
| -# SIGTERM to the app, SIGKILL to anything it left behind after |
| -# TimeoutStopSec; the activator's stop budget is 20s. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the app as its child (#126): it forwards the SIGTERM and exits |
diff --git a/internal/host/units/carlos-instance-seed@.service b/internal/host/units/carlos-instance-seed@.service| index 72f162fd8..9878ffcfc 100644 |
| --- a/internal/host/units/carlos-instance-seed@.service |
| +++ b/internal/host/units/carlos-instance-seed@.service |
| @@ -38,9 +38,14 @@ Group=carlos |
| # carries the tier (§4.3, §6.6). Slices referenced here are created on |
| # demand; no per-host unit file exists. |
| Slice=carlos-instances-%i.slice |
| -# SIGTERM to the app, SIGKILL to anything it left behind after |
| -# TimeoutStopSec; the activator's stop budget is 20s. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # No SuccessExitStatus= here, unlike the run and sidecar templates (#126): |
| # the trampoline's init reports a seed a signal ended as 128+n, and a seed |
diff --git a/internal/host/units/carlos-instance-sidecar@.service b/internal/host/units/carlos-instance-sidecar@.service| index befa84c4a..d1263c0d6 100644 |
| --- a/internal/host/units/carlos-instance-sidecar@.service |
| +++ b/internal/host/units/carlos-instance-sidecar@.service |
| @@ -34,9 +34,14 @@ Slice=carlos-instances-%i.slice |
| # The activator owns recycling: a crash stands the entry down and the next |
| # request wakes it. systemd must not race that. |
| Restart=no |
| -# SIGTERM to the app, SIGKILL to anything it left behind after |
| -# TimeoutStopSec; the activator's stop budget is 20s. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the app as its child (#126): it forwards the SIGTERM and exits |
diff --git a/internal/host/units/carlos-instance-tty@.service b/internal/host/units/carlos-instance-tty@.service| index 168f4e195..e99165624 100644 |
| --- a/internal/host/units/carlos-instance-tty@.service |
| +++ b/internal/host/units/carlos-instance-tty@.service |
| @@ -28,8 +28,14 @@ ExecStopPost=/opt/carlos/carlos instance-exit tty %I |
| User=carlos |
| Group=carlos |
| Slice=carlos-instances-%i.slice |
| -# SIGTERM to the session, SIGKILL to anything left 20 s later. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # No SuccessExitStatus=: the agent reads the exit status itself. |
| # One socket, the agent's: PID 1 connects stdin to it as root, outside the |
diff --git a/internal/host/units/carlos-instance@.service b/internal/host/units/carlos-instance@.service| index 750d06d33..2d4e94295 100644 |
| --- a/internal/host/units/carlos-instance@.service |
| +++ b/internal/host/units/carlos-instance@.service |
| @@ -34,9 +34,14 @@ Slice=carlos-instances-%i.slice |
| # The activator owns recycling: a crash stands the entry down and the next |
| # request wakes it. systemd must not race that. |
| Restart=no |
| -# SIGTERM to the app, SIGKILL to anything it left behind after |
| -# TimeoutStopSec; the activator's stop budget is 20s. |
| -KillMode=mixed |
| +# SIGTERM to every process in the unit, SIGKILL to anything left |
| +# TimeoutStopSec later; the activator's stop budget is 20s. Not mixed |
| +# (#528): under PrivatePIDs= systemd sometimes takes the main process for |
| +# "not our child", and then a mixed stop, which sends its SIGTERM to the |
| +# main process alone, has nothing to wait on and SIGKILLs at once. Here |
| +# the SIGTERM reaches the tenant directly as well, so there is always |
| +# something to wait on. |
| +KillMode=control-group |
| TimeoutStopSec=20 |
| # The main process is the trampoline, which stays as the namespace's init |
| # and runs the app as its child (#126): it forwards the SIGTERM and exits |
diff --git a/internal/host/units_isolation_test.go b/internal/host/units_isolation_test.go| index 867706119..c4b3e7015 100644 |
| --- a/internal/host/units_isolation_test.go |
| +++ b/internal/host/units_isolation_test.go |
| @@ -276,25 +276,25 @@ func TestInstanceTemplatesShareOneNamespace(t *testing.T) { |
| // drop-in 50-workdir-incident-256.conf carries this exact line, |
| // so removing it changes nothing. |
| "WorkingDirectory=/data/instances/%I", |
| - "Slice=carlos-instances-%i.slice", "Restart=no", "KillMode=mixed", "TimeoutStopSec=20", "BindsTo=carlos-edge.service", |
| + "Slice=carlos-instances-%i.slice", "Restart=no", "KillMode=control-group", "TimeoutStopSec=20", "BindsTo=carlos-edge.service", |
| "Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount", "OOMScoreAdjust=500", "OOMPolicy=kill", "TasksMax=256", "Type=simple", |
| // The trampoline's init exits 128+n for a death by signal n (#126). |
| "SuccessExitStatus=129 130 141 143"}, |
| "carlos-instance-gen@.service": {"ExecStart=/opt/carlos/carlos instance-exec run %I", "ExecStopPost=/opt/carlos/carlos instance-exit run %I", |
| - "Restart=no", "KillMode=mixed", "TimeoutStopSec=20", "BindsTo=carlos-edge.service", |
| + "Restart=no", "KillMode=control-group", "TimeoutStopSec=20", "BindsTo=carlos-edge.service", |
| "Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount", "OOMScoreAdjust=500", "OOMPolicy=kill", "TasksMax=256", "Type=simple", |
| "SuccessExitStatus=129 130 141 143"}, |
| "carlos-instance-seed@.service": {"Type=oneshot", "RemainAfterExit=yes", "TimeoutStartSec=120", "ExecStart=/opt/carlos/carlos instance-exec seed %I", |
| "ExecStopPost=/opt/carlos/carlos instance-exit seed %I", "StandardOutput=file:/run/carlos/logs/%I.seed.sock", |
| - "KillMode=mixed", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"}, |
| + "KillMode=control-group", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"}, |
| "carlos-instance-cmd@.service": {"Type=oneshot", "RemainAfterExit=yes", "TimeoutStartSec=6h5m", "ExecStart=/opt/carlos/carlos instance-exec cmd %I", |
| "ExecStopPost=/opt/carlos/carlos instance-exit cmd %I", "StandardOutput=file:/run/carlos/logs/%I.cmd.sock", |
| - "StandardError=file:/run/carlos/logs/%I.cmd-err.sock", "KillMode=mixed", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"}, |
| + "StandardError=file:/run/carlos/logs/%I.cmd-err.sock", "KillMode=control-group", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"}, |
| // One interactive session (rootfs Plan 6): the cmd unit's shape, |
| // stdin from the agent's socket and no SuccessExitStatus=. |
| "carlos-instance-tty@.service": {"Type=oneshot", "RemainAfterExit=yes", "TimeoutStartSec=6h5m", |
| "ExecStart=/opt/carlos/carlos instance-exec tty %I", "StandardInput=file:/run/carlos/logs/%I.tty.sock", "Slice=carlos-instances-%i.slice", |
| - "KillMode=mixed", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I", "User=carlos"}, |
| + "KillMode=control-group", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I", "User=carlos"}, |
| "carlos-instance-sidecar@.service": {"Type=simple", "ExecStart=/opt/carlos/carlos instance-exec sidecar %I", |
| "ExecStopPost=/opt/carlos/carlos instance-exit sidecar %I", "StandardOutput=file:/run/carlos/logs/%I.sidecar.sock", |
| "SuccessExitStatus=129 130 141 143", "WorkingDirectory=/data/instances/%I"}, |
| @@ -477,7 +477,7 @@ func TestCmdTemplateEqualsSeedOutsideTheNamedLines(t *testing.T) { |
| t.Fatalf("directive %d: cmd template has %q, want %q (the seed's line with the named swaps)", i, g, w) |
| } |
| } |
| - for _, keep := range []string{"Type=oneshot", "RemainAfterExit=yes", "KillMode=mixed", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"} { |
| + for _, keep := range []string{"Type=oneshot", "RemainAfterExit=yes", "KillMode=control-group", "TimeoutStopSec=20", "WorkingDirectory=/data/instances/%I"} { |
| if !hasDirective(readUnit(t, "carlos-instance-cmd@.service"), keep) { |
| t.Errorf("cmd template lacks %q", keep) |
| } |
diff --git a/internal/host/units_privatepids_test.go b/internal/host/units_privatepids_test.go| new file mode 100644 |
| index 000000000..78ca899ce |
| --- /dev/null |
| +++ b/internal/host/units_privatepids_test.go |
| @@ -0,0 +1,54 @@ |
| +package host |
| + |
| +import ( |
| + "io/fs" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// TestPrivatePIDsUnitsKillControlGroup is #528's invariant. Under |
| +// PrivatePIDs=yes systemd 257 loses a race on a share of starts and marks |
| +// the unit's main process "not our child" (alien). At stop it then has |
| +// nothing to wait for after the SIGTERM unless the SIGTERM phase also |
| +// signals the rest of the cgroup, and it goes straight to SIGKILL: |
| +// Result=signal and no drain. KillMode=mixed and KillMode=process signal |
| +// only the main process in that phase, so neither may sit beside |
| +// PrivatePIDs=yes. control-group (systemd's default when no line is set) |
| +// signals every process, and the app under the trampoline is always one. |
| +func TestPrivatePIDsUnitsKillControlGroup(t *testing.T) { |
| + t.Parallel() |
| + units := map[string]string{"claimBoundTemplate": claimBoundTemplate} |
| + err := fs.WalkDir(unitsFS, "units", func(path string, d fs.DirEntry, err error) error { |
| + if err != nil || d.IsDir() { |
| + return err |
| + } |
| + b, err := unitsFS.ReadFile(path) |
| + if err != nil { |
| + return err |
| + } |
| + units[path] = string(b) |
| + return nil |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + checked := 0 |
| + for name, u := range units { |
| + if !hasDirective(u, "PrivatePIDs=yes") { |
| + continue |
| + } |
| + checked++ |
| + for _, d := range directives(u) { |
| + if strings.HasPrefix(d, "KillMode=") && d != "KillMode=control-group" { |
| + t.Errorf("%s: %s beside PrivatePIDs=yes: an alien main PID makes every stop a SIGKILL (#528)", name, d) |
| + } |
| + } |
| + } |
| + // Eleven tenant templates (run, gen, seed, sidecar, cmd and tty, and the |
| + // rootfs run, gen, worker, cmd and tty), two probes, the demo build and |
| + // snapshot units and the claim-bound template: a walk that finds fewer |
| + // has stopped reading the units. |
| + if checked < 16 { |
| + t.Fatalf("checked %d PrivatePIDs units; the walk lost the templates", checked) |
| + } |
| +} |
diff --git a/internal/host/units_rootfs_test.go b/internal/host/units_rootfs_test.go| index b55325bc4..3996fe46b 100644 |
| --- a/internal/host/units_rootfs_test.go |
| +++ b/internal/host/units_rootfs_test.go |
| @@ -219,7 +219,7 @@ func TestRootfsTemplatesPerUnitLines(t *testing.T) { |
| common := []string{"BindsTo=carlos-edge.service", |
| "Requires=data-instances.mount run-carlos-instances.mount run-carlos-logs.mount", |
| "After=carlos-edge.service data-instances.mount run-carlos-instances.mount run-carlos-logs.mount", |
| - "KillMode=mixed", "TimeoutStopSec=20", "TasksMax=256", "OOMScoreAdjust=500", "OOMPolicy=kill", "User=carlos", "Group=carlos"} |
| + "KillMode=control-group", "TimeoutStopSec=20", "TasksMax=256", "OOMScoreAdjust=500", "OOMPolicy=kill", "User=carlos", "Group=carlos"} |
| for name, wants := range map[string][]string{ |
| "carlos-instance-rootfs@.service": append([]string{"Type=simple", "ExecStart=/opt/carlos/carlos instance-exec --rootfs run %I", "ExecStopPost=/opt/carlos/carlos instance-exit --rootfs run %I", |
| "RootImage=/run/carlos-control/exe/%I/rootfs/run/image", |
diff --git a/internal/instanceexec/init_linux.go b/internal/instanceexec/init_linux.go| index eb5717c9f..57f82e786 100644 |
| --- a/internal/instanceexec/init_linux.go |
| +++ b/internal/instanceexec/init_linux.go |
| @@ -29,12 +29,14 @@ import ( |
| // keeps its default actions exactly as on the flat layout, and does the |
| // two things PID 1 owes the namespace: |
| // |
| -// - Forward. KillMode=mixed sends the stop's SIGTERM to the main |
| -// process, which is this one; it and the other signals in forwarded |
| -// go on to the tenant, which acts on them as it would have as the |
| -// main process. SIGKILL needs no forwarding: after TimeoutStopSec |
| -// systemd sends it to the whole cgroup, and SIGKILL to a namespace |
| -// init from outside always kills it. |
| +// - Forward. A SIGTERM or other signal in forwarded that reaches this |
| +// process goes on to the tenant, which acts on it as it would have |
| +// as the main process. A unit stop (KillMode=control-group, #528) |
| +// signals the tenant directly too, so it may see that SIGTERM twice; |
| +// a claims-on generation's Retire signals this process alone, which |
| +// is why the forward stays. SIGKILL needs no forwarding: after |
| +// TimeoutStopSec systemd sends it to the whole cgroup, and SIGKILL |
| +// to a namespace init from outside always kills it. |
| // - Reap. A process whose parent dies is reparented to the namespace's |
| // init, and stays a zombie until init waits for it. The tenant's own |
| // children that outlive their parent land here. |