Skip to content

refactor(daemon): reap a session worker in one step and name how a session ended - #7716

Merged
oferchen merged 1 commit into
masterfrom
fork-f2b-reap
Sep 6, 2026
Merged

oferchen merged 1 commit into
masterfrom
fork-f2b-reap

Conversation

@oferchen

@oferchen oferchen commented Sep 6, 2026

Copy link
Copy Markdown
Owner

FORK-EPIC F2b, step 1. Behaviour-neutral, and the last structural blocker between the SessionWorker seam #7715 introduced and a fork-backed variant of it. F2 is what closes the largest actionable UTS-3.5.0 cluster — 4 cells x 3 legs = 12 of the 41 residual fail rows — because the daemon serves connections in threads, so chroot() leaks process-wide.

The one thing a thread can do that a forked child cannot

is_finished() then join() is two steps. waitpid(pid, …, WNOHANG) is one: it reports the child's exit and consumes its status in the same call. Split across a predicate and a later join, the predicate would throw away the outcome it had just collected and the join would get ECHILD.

So the two collapse into

fn try_reap(self) -> Result<SessionOutcome, Self>

Consuming self also states the ownership rule the connection slot depends on: reaping a worker is what drops its max connections guard, which #7715 made parent-owned. reap_finished_workers drains and rebuilds instead of indexing, so a reaped worker is moved out and dropped while a still-running one is handed back untouched. SessionWorker deliberately no longer offers a borrowing readiness predicate for a caller to misuse — the tests read the backing handle directly, and that asymmetry is the point.

Naming the outcome

thread::Result<WorkerResult> spelled the backing into every signature — precisely what a fork-backed variant has to replace. The three ways a session can end become a named SessionOutcome:

variant today under fork
Ok returned normally _exit(0)
Failed(peer, error) returned Err non-zero exit
Died(String) panic escaped catch_unwind fatal signal

join_backing is now the only site that knows a thread is involved.

upstream: socket.c:676-684 sigchld_handler() reaps with waitpid(-1, NULL, WNOHANG) — a NULL status pointer, so upstream's parent discards the session's fate entirely. oc reports it instead, which is why the outcome needs a name; per #7590 none of its variants is fatal to the listener.

Why this is behaviour-neutral, proven not asserted

#7715's two tests keep their distinct kill signatures across the refactor — which is the evidence that the guarantees survived, not just that the suite is green:

  • leak the slot through the reap → both fail
  • drop the reap-before-capacity call → only admission_reaps_before_consulting_the_connection_cap fails, a_finished_worker_holds_its_slot_until_reaped stays green

Verification

On the pinned 1.88.0 toolchain (not stable — that mismatch was the shipping defect in #7655):

  • daemon 1991/1991, 6 skipped
  • workspace clippy --all-targets --all-features --no-deps clean
  • tools/ci/check_rustfmt_all.py clean, 3422 files (the whole-tree gate, not cargo fmt --all)

Next

F2b step 2 adds the fork-backed variant. Two requirements read from socket.c:752-766 that the epic brief never named, and they are what makes that step larger than this one:

  1. The child must close every listening socket and the pid-file fd, or a child outliving the parent keeps the port bound. oc's listeners live in PollAcceptEngine.listeners, which handle_accepted_connection cannot reach — that plumbing is the real cost.
  2. Per-pid reaping, never waitpid(-1). The daemon spawns non-session children for name converter, pre/post-xfer exec and auth; a bulk reaper would steal their statuses and make Child::wait() fail ECHILD. session_fork::reap_finished_children is therefore not the function to wire up as shipped.

One open question for step 2: upstream sleeps 2s on fork failure as an anti-spin measure. This repo's policy is no retry/backoff, and oc's next loop action is a blocking poll(…, -1), so I lean toward omitting it and documenting why rather than porting it silently.

…ssion ended

Two coupled changes to the `SessionWorker` seam, both behaviour-neutral under
the current thread backing and both prerequisites for a forked one.

`is_finished()` + `join_worker(handle)` collapse into
`SessionWorker::try_reap(self) -> Result<SessionOutcome, Self>`. Observing that
a session ended and collecting its outcome are ONE step, not two. A thread
backing can split them; a forked child cannot, because
`waitpid(pid, .., WNOHANG)` reports the child's exit AND consumes its status in
the same call - a separate predicate would discard the outcome it had just
collected, and a second call would get ECHILD. Keeping them together is what
lets the backing change without every caller changing with it.

Consuming `self` also states the ownership rule the connection slot depends on:
reaping a worker is what drops its `max connections` guard, which #7715 made
parent-owned. `reap_finished_workers` therefore drains and rebuilds rather than
indexing, so the reaped worker is moved out and dropped while a still-running
one is handed back untouched. `SessionWorker` deliberately no longer offers a
borrowing readiness predicate for a caller to misuse.

The three ways a session can end become a named `SessionOutcome` - Ok, Failed,
Died - rather than `thread::Result<WorkerResult>`, whose shape is a property of
the thread backing alone and is exactly what a fork-backed variant has to
replace. `Died` covers a panic that escaped `catch_unwind` today and a fatal
signal once a child serves the session. `join_backing` is now the only site
that knows a thread is involved.

upstream: socket.c:676-684 `sigchld_handler()` reaps with
`waitpid(-1, NULL, WNOHANG)` - a NULL status pointer, so upstream's parent
discards the session's fate entirely. oc reports it instead, which is why the
outcome needs a name; per #7590 none of its variants is fatal to the listener.

No behaviour change is intended, and the proof is that #7715's two tests keep
their distinct kill signatures across the refactor:

  - leak the slot through the reap -> BOTH fail
  - drop the reap-before-capacity call -> ONLY
    `admission_reaps_before_consulting_the_connection_cap` fails

Verified on the pinned 1.88.0 toolchain: daemon 1991/1991 (6 skipped),
workspace clippy --all-targets --all-features clean, whole-tree rustfmt clean
across 3422 files.
@oferchen
oferchen enabled auto-merge (squash) September 6, 2026 19:22
@oferchen
oferchen merged commit 5cdb918 into master Sep 6, 2026
78 checks passed
@oferchen
oferchen deleted the fork-f2b-reap branch September 6, 2026 20:07
oferchen added a commit that referenced this pull request Sep 6, 2026
…child

`reap_finished_children` swept with `waitpid(-1, .., WNOHANG)` and returned
only a count. Wiring that into the accept loop - which is exactly what the
comment at connection.rs:314 pointed a future reader at - would have been a
latent defect, for two independent reasons.

It steals other children's statuses. The daemon forks children that are not
sessions: the `name converter` helper, the pre-/post-transfer `exec` hooks and
the authentication helper, all owned by `std::process::Child` and reaped
through its own `wait`. A bulk sweep in the accept loop races those owners,
and the loser gets ECHILD - a helper whose exit code the daemon reads would
silently stop being observable.

And a count cannot release a slot. #7715 made the `max connections` guard
parent-owned and #7716 made reaping the act that drops it, so the parent has
to learn WHICH worker ended, not how many did.

Both dissolve if the pid is a parameter:

    try_reap(child_pid)      -> Result<Option<ChildEnd>>
    wait_for_child(child_pid) -> Result<ChildEnd>

That pair is deliberately the same shape as the `SessionWorker::try_reap` /
`wait` pair #7716 introduced, so the fork backing can slot in under the
existing seam. `ChildEnd` stays process vocabulary - Exited or Signalled -
because what a non-zero exit means for a *session* is the daemon's decision,
not this module's. Under fork, `Died` becomes a fatal signal rather than an
escaped panic, which is why the two layers must not share one enum.

upstream: socket.c:676-684 `sigchld_handler()` does use `waitpid(-1, ..)`, but
passes a NULL status pointer - it discards the outcome, so it has nothing to
lose to a race. oc reports session outcomes, so it needs the status and
therefore the narrower call.

`wait_for_child` resumes on EINTR. That is not the retry/backoff this repo
bars: the syscall never completed and nothing is being backed off. A daemon
with signal handlers installed sees it routinely.

Still no production caller - the module remains staged for FORK-EPIC F2b step
2. The change is that the API it stages is now the one that step can safely
use, and the footgun is gone rather than waiting to be picked up.
oferchen added a commit that referenced this pull request Sep 6, 2026
…child (#7718)

`reap_finished_children` swept with `waitpid(-1, .., WNOHANG)` and returned
only a count. Wiring that into the accept loop - which is exactly what the
comment at connection.rs:314 pointed a future reader at - would have been a
latent defect, for two independent reasons.

It steals other children's statuses. The daemon forks children that are not
sessions: the `name converter` helper, the pre-/post-transfer `exec` hooks and
the authentication helper, all owned by `std::process::Child` and reaped
through its own `wait`. A bulk sweep in the accept loop races those owners,
and the loser gets ECHILD - a helper whose exit code the daemon reads would
silently stop being observable.

And a count cannot release a slot. #7715 made the `max connections` guard
parent-owned and #7716 made reaping the act that drops it, so the parent has
to learn WHICH worker ended, not how many did.

Both dissolve if the pid is a parameter:

    try_reap(child_pid)      -> Result<Option<ChildEnd>>
    wait_for_child(child_pid) -> Result<ChildEnd>

That pair is deliberately the same shape as the `SessionWorker::try_reap` /
`wait` pair #7716 introduced, so the fork backing can slot in under the
existing seam. `ChildEnd` stays process vocabulary - Exited or Signalled -
because what a non-zero exit means for a *session* is the daemon's decision,
not this module's. Under fork, `Died` becomes a fatal signal rather than an
escaped panic, which is why the two layers must not share one enum.

upstream: socket.c:676-684 `sigchld_handler()` does use `waitpid(-1, ..)`, but
passes a NULL status pointer - it discards the outcome, so it has nothing to
lose to a race. oc reports session outcomes, so it needs the status and
therefore the narrower call.

`wait_for_child` resumes on EINTR. That is not the retry/backoff this repo
bars: the syscall never completed and nothing is being backed off. A daemon
with signal handlers installed sees it routinely.

Still no production caller - the module remains staged for FORK-EPIC F2b step
2. The change is that the API it stages is now the one that step can safely
use, and the footgun is gone rather than waiting to be picked up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant