Skip to content

execd: end a job from the kernel's child list, and stop allocating per process in the scan - #748

Merged
capy-ai[bot] merged 1 commit into
mainfrom
capy/end-a-job-from
Nov 27, 2026
Merged

capy-ai[bot] merged 1 commit into
mainfrom
capy/end-a-job-from

Conversation

@jaredLunde

Copy link
Copy Markdown
Contributor

One full /proc scan of every process on the box happened once per job-life event (proc::members), so a cycle's cost
tracked the sandbox's process table rather than the job: at 731 processes, 28.7 ms per start→ended→remove cycle,
1 467 /proc opens and 13 436 allocations. Both halves of that are gone.

A job this server spawned now ends from the kernel's child list. execd is the subreaper, so every process of a job
it spawned descends from it, and a live process's ancestors are live — the topmost is a live child of execd, or the
survivors were adopted by the subreaper, which is execd again. So waitid(P_ALL, WEXITED|WNOHANG|WNOWAIT) == ECHILD (no
child at all, a zombie included) proves nothing of the job is running. WNOWAIT is waitid's — waitpid rejects it
with EINVAL — and it leaves a zombie waitable, so the check never collects a status the reaper must deliver.
jobs.rs:2547, jobs.rs:2564 → proc::job_gone_owned (proc.rs:497).

The scan is still the answer wherever it is needed, unchanged in behaviour: whenever the child list is not empty
(another job, a terminal's keeper, anything execd adopted — which is what a living stray always is), for a job a restart
adopted (supervise_adopted: a previous execd spawned it, so it is not this process's descendant), on the slow tick, and
for everything that must name processes (kill, wipe, survivors, terminals). The end still goes through
kernel::step(Ev::GroupEmpty) in the same place.

The scan no longer allocates per process. proc::scan reuses one Scratch: a path String, the
EXECD_JOB_MARK=<mark> bytes, and one read buffer, instead of a format! and an fs::read per file per process. Same
pids, same filter, same syscalls.

Measured on the real release binary (benches/proc_scan.py, new), one job at a time, 3 warm-up + 10 cycles, p50:

processes cycle p50 cycles/s /proc opens per cycle proc.rs allocations per cycle
107 → 100 6.48 → 3.49 ms 154 → 286 217.6 → 0 1 559 → 308
329 → 322 12.98 → 3.56 ms 77 → 281 662.8 → 0 5 755 → 307
731 → 724 28.67 → 3.72 ms 34.9 → 269 1 466.9 → 0 13 436 → 309

With one long-lived job beside the churn (~734 processes) the child list cannot answer and the scan still runs, twice per
cycle there: 30.0 → 20.0 ms, /proc opens 2 759 → 2 580 (no win — stated, not hidden), allocations 28 756 → 3 603 and
bytes 7.89 MB → 133 KB, which is part 1 alone.

The benchmark carries the gate: --assert-proc-opens 1 --assert-flat 1.3 passes on this head and fails every row on the
pre-change binary (1 494 /proc opens per cycle at 746, allocations growing 8.03× across the process counts).

One behaviour changes, and it is documented. A process that carries a job's mark without being a descendant of execd
— a client can read the mark off the wire and put it on a process of its own — no longer holds that job open. Every
process that got the mark the documented way inherited it and is a descendant. The state before this change is kept as
the test-only mutant mut_end_scan_always, not as a shipped switch: no rollback flag (CREW rule 6), and nothing new is
written to disk so same-build crash recovery is untouched.

Proofs. tests/job_escape.rs gains a double-forked daemon, a grandchild that outlives its parent, a stray under
EXECD_RUN_AS, a check that the fast path fires for a quiet end and stands aside for a busy one, and the forged-mark
case. mut_end_group_only (a gone group reads as the end) turns the three stray tests red;
mut_end_scan_always (the fast path removed) turns the fast-path test red. All 32 execd suites pass at this head
(including differential's 40 random programs), with cargo fmt --check, dprint check and
clippy --all-targets -- -D warnings clean on the pinned 1.92 toolchain.

ARCHITECTURE.md gains "The end of a job" under Process model, the tests-table row, and the changed mark caveat.

Open in Capy

…uffers

A job-life cycle paid one full `/proc` scan: at 731 processes, 28.7 ms, 1 467 opens and
13 436 allocations per start -> ended -> remove. Two changes, both counted with
`benches/proc_scan.py`:

- `proc::scan` reuses one `Scratch` (path, mark bytes, read buffer) instead of a `format!`
  and an `fs::read` per file per process: allocations per cycle 5 755 -> 307 at 333
  processes, 13 436 -> 309 at 733, flat.
- A job this server spawned ends from the kernel's child list: where execd is the
  subreaper every process of the job is its descendant, so `waitid(P_ALL,
  WEXITED|WNOHANG|WNOWAIT) == ECHILD` answers the end without reading `/proc`.
  `WNOWAIT` is `waitid`'s (waitpid rejects it) and leaves a zombie waitable, so the
  check never takes a status the reaper owes. Cycle 28.67 -> 3.72 ms and 1 467 -> 0
  `/proc` opens at 731 processes.

The scan is still the answer whenever the child list is not empty (another job, a keeper,
an adopted stray), for a job a restart adopted, and for everything that names processes.
A process carrying the mark without being a descendant of execd no longer holds its job
open; `mut_end_scan_always` keeps the old path as a test feature rather than a switch.

tests/job_escape.rs pins each invariant and goes red under `mut_end_group_only` and
`mut_end_scan_always`. ARCHITECTURE.md documents the decision and the gate chain.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai
capy-ai Bot added this pull request to the merge queue Nov 27, 2026
Merged via the queue into main with commit 8a22dcc Nov 27, 2026
34 checks passed
@capy-ai
capy-ai Bot deleted the capy/end-a-job-from branch November 27, 2026 21:40
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