Repository navigation
refactor(daemon): reap a session worker in one step and name how a session ended - #7716
Merged
Merged
Conversation
…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
enabled auto-merge (squash)
September 6, 2026 19:22
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FORK-EPIC F2b, step 1. Behaviour-neutral, and the last structural blocker between the
SessionWorkerseam #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, sochroot()leaks process-wide.The one thing a thread can do that a forked child cannot
is_finished()thenjoin()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 getECHILD.So the two collapse into
Consuming
selfalso states the ownership rule the connection slot depends on: reaping a worker is what drops itsmax connectionsguard, which #7715 made parent-owned.reap_finished_workersdrains and rebuilds instead of indexing, so a reaped worker is moved out and dropped while a still-running one is handed back untouched.SessionWorkerdeliberately 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 namedSessionOutcome:Ok_exit(0)Failed(peer, error)ErrDied(String)catch_unwindjoin_backingis now the only site that knows a thread is involved.upstream:
socket.c:676-684sigchld_handler()reaps withwaitpid(-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:
admission_reaps_before_consulting_the_connection_capfails,a_finished_worker_holds_its_slot_until_reapedstays greenVerification
On the pinned 1.88.0 toolchain (not stable — that mismatch was the shipping defect in #7655):
clippy --all-targets --all-features --no-depscleantools/ci/check_rustfmt_all.pyclean, 3422 files (the whole-tree gate, notcargo fmt --all)Next
F2b step 2 adds the fork-backed variant. Two requirements read from
socket.c:752-766that the epic brief never named, and they are what makes that step larger than this one:PollAcceptEngine.listeners, whichhandle_accepted_connectioncannot reach — that plumbing is the real cost.waitpid(-1). The daemon spawns non-session children forname converter, pre/post-xfer exec and auth; a bulk reaper would steal their statuses and makeChild::wait()failECHILD.session_fork::reap_finished_childrenis 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.