Place supervised module children in delegated cgroups (closes #97)

Closed
IC
2mo ago
+403-14

Closes #97. Placement only, no limit keys, per your re-scope.

The unsafe stays out of the daemon

subc-daemon and subc-core both forbid(unsafe_code), and forbid cannot be locally allowed — that is the whole constraint. New leaf crate crates/subc-cgroup: publish = false, no subc-* dependencies, #![cfg(target_os = "linux")] at the root so it is empty everywhere else, #![deny(unsafe_code)] with exactly one #[allow(unsafe_code)] on the pre_exec closure factory. The comment there names async-signal-safety as the actual invariant rather than the keyword, and the path string is formatted before the closure exists so nothing allocates between fork and exec.

tokio is taken as an external crates.io dependency so apply() can call pre_exec beside its own allow. "Zero workspace dependencies" is satisfied in the sense that matters — no subc-* edges — and I did not want to silently reinterpret it, so: that is the reading I built to.

Not-delegated and delegated-but-broken are different branches

Your override constraint decides the shape here. prepare_current() creates and removes a probe subgroup at startup:

  • No cgroup v2, or PermissionDenied → Ok(None) → no placement, ordinary spawns, one WARN naming Delegate=yes. An unconfigured host starts and runs exactly as before.
  • Unexpected I/O error → also falls back to None and warns, because a probe failure must not stop a daemon starting.
  • Delegation confirmed, then the module path or the cgroup.procs write fails → that is pin 4, a real spawn failure carrying the cgroup path.

The two failure warnings are deliberately different text. Ok(None) gets the Delegate=yes guidance; the error arm gets "disabled by an unexpected cgroup probe error" plus the error. Telling an operator to set Delegate=yes when delegation is already fine is the failure mode #104 cost an hour to, and I was not going to ship it in my own change.

ck setup writes Delegate=yes and DelegateSubgroup=daemon into the [Service] section it already generates.

Verification

Three tests in the leaf crate — placement read back from /proc/<pid>/cgroup, a failing cgroup.procs write surfacing as the spawn error, and the delegation probe — each skipping with a named printed reason when the host cannot exercise it.

The test guarding your override constraint is in subc-daemon instead, and that placement is deliberate. My first version lived in the leaf crate, skipped on any delegated host (so: nowhere in this fleet), and when it did run it spawned true and asserted success — which is Command::spawn working, not the daemon's decision path. It stayed green when the branch it guarded was severed. The replacement spawns fake-aft-stub through the supervisor with cgroup_placement: None, supplying the condition rather than detecting it, so it runs on delegated and undelegated hosts alike. I mutation-proved it myself:

thread '...no_cgroup_placement_does_not_block_fake_aft_stub_spawn' panicked at supervise.rs:5332:
no delegation must not turn an otherwise valid spawn into a failure:
  Err(Cgroup { module_id: "no-cgroup-placement",
               source: Custom { error: "mutation: no-placement branch rejected" } })

Cross-platform checked by construction, since local Linux gates structurally cannot see it (the #59 class, and there is no rustup on this box to cross-compile): flipping target_os = "linux" → "windows" in the two source files makes every Linux-gated block inactive and every not-Linux block active on this host — the shape Windows and macOS compile. cargo check -p subc-daemon --all-targets passes, with a positive control confirming that harness catches real errors in those files. Re-run after the rebase, since the files changed crates.

Gates: workspace tests 0 failures · clippy -D warnings · fmt · check-wire-crate-versions.sh 6 crates none unbumped · porcelain clean.

Rebase note

Master moved 38 commits under this, including the subc-core → subc-daemon split that relocated supervise.rs and bootstrap.rs. Six conflicts, all keep-both — your terminal_journal, terminal_journal_path and the capture_logs_dir rebind each landing beside my placement lines, none actually contradictory. The dependency moved with the files, from subc-core/Cargo.toml to subc-daemon/Cargo.toml; a rebase does not follow a dependency across a crate boundary, so that one was manual. Both crates bumped to 0.18.16 with the three dependent version requirements updated, and I verified after resolution that both sides of every conflict survived rather than trusting the merge.

Incidentally, the comment you added at bootstrap.rs:583 is the same defect I filed as #106's second half, found from Broca's side within hours. Their version is worse than mine: mine was zero-byte fixture files, theirs was a rig spawning a module actually named broca and appending to the production capture file they count seal lines in.

CONSUMER-IMPACT: supervised module children are placed in a per-module cgroup subtree when the daemon's cgroup is delegated. Undelegated hosts are unaffected beyond one startup warning. No new configuration keys.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Closes #97: supervised module children on Linux are now placed into per-module cgroups when the daemon’s cgroup is delegated. Previously children were spawned without placement; now they land under a subc-modules subtree. Undelegated hosts are unaffected beyond one startup warning. No new configuration keys.

Details

  • Adds subc-cgroup, a Linux-only leaf crate holding the placement logic; it contains the only unsafe block so subc-daemon and subc-core stay forbid(unsafe_code).
  • ck setup writes Delegate=yes and DelegateSubgroup=daemon into the generated systemd unit.
  • Unexpected probe errors fall back to no placement with a distinct warning; real spawn failures surface as errors naming the cgroup path.
  • Bumps subc-daemon to 0.18.16 and subc-core to 0.18.18; path-dependency consumers’ lockfiles gain subc-cgroup as a transitive dependency.