Skip to content

refactor(daemon): give the accept loop a session-worker seam and park the connection slot in the parent - #7715

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

oferchen merged 1 commit into
masterfrom
fork-f2a

Conversation

@oferchen

@oferchen oferchen commented Sep 6, 2026

Copy link
Copy Markdown
Owner

FORK-EPIC F2a. Behaviour-neutral groundwork for converting the daemon's per-connection worker from a thread to a forked child, which is what closes the largest actionable UTS-3.5.0 cluster: 4 cells x 3 legs = 12 of the 41 residual fail rows (daemon-chroot, daemon-chroot-munge-default, daemon-module-private-parent, daemon-symlink-escape-matrix). Root cause there is that the daemon serves connections in threads, so chroot() leaks process-wide.

F2's precondition — fork_session's "fork from a single-threaded accept path" — became satisfiable when #7681 collapsed the portable engines into one poll(2) accept thread. This PR closes the one residual that record names: the max connections guard.

What changes

AcceptLoopState.workers becomes Vec<SessionWorker>, pairing each in-flight connection's backing handle with the slot it occupies, and the guard moves out of the worker closure into that parent-owned struct.

A forked child inherits a copy of everything the session owns. A guard moved into the session would be released by the child and never by the parent, so max connections would silently stop being enforced — with nothing failing. Putting the slot where the reap releases it, before any fork exists, is the point of this commit.

The non-obvious half

Moving the guard is not behaviour-neutral on its own. Per iteration:

reap -> poll/accept (BLOCKS) -> refuse_if_at_capacity -> spawn

Today the guard drops the instant the thread exits, including while the loop is blocked in poll. Under parent ownership it drops only at the next iteration's reap — so a session that ends during the poll keeps its slot, and a connection accepted in that window is refused where master admits it. Narrow, real, and it would have surfaced later as a flaky max connections test rather than as this change.

So handle_accepted_connection now reaps immediately before consulting the cap. Under a forked backing that same call site is where reap_finished_children belongs.

Tests — two, discriminating differently

Test Pins
a_finished_worker_holds_its_slot_until_reaped where the slot is released
admission_reaps_before_consulting_the_connection_cap the ordering

state.served is the discriminator for the second: the refusal arm returns before the increment, and both arms return false, so the return value cannot tell them apart.

Mutation results:

  • remove the reap-before-capacity call -> only admission_reaps fails (left: 0, right: 1, i.e. refused)
  • make the reap leak the slot -> both fail, since both depend on the reap releasing it

Without the second test that new call would have been an unproven line.

Verification

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

Next: F2b adds the fork-backed variant (sync engine only — the async engine refuses privileged modules at startup, so chroot() can never run there), F2c flips it and the 12 manifest rows in one commit.

… the connection slot in the parent

`AcceptLoopState.workers` becomes `Vec<SessionWorker>`, pairing each in-flight
connection's backing handle with the `max connections` slot it occupies. The
slot guard moves out of the worker closure and into that parent-owned struct.

Why the guard has to live there: the daemon still serves connections in
threads, so `chroot()` leaks process-wide and four upstream-testsuite cells
fail (`daemon-chroot`, `daemon-chroot-munge-default`,
`daemon-module-private-parent`, `daemon-symlink-escape-matrix`). Converting
those to forked children is the fix, and `crates/platform/src/session_fork.rs`
now has a satisfiable precondition since the accept path became single-threaded.
But a forked child inherits a COPY of everything the session owns, so a guard
moved into the session would be released by the child and never by the parent -
`max connections` would silently stop being enforced with nothing failing. This
commit puts the slot where the reap can release it, before any fork exists.

The second hunk is what keeps the first behaviour-neutral, and it is not
optional. Per-iteration the loop runs

    reap -> poll/accept (BLOCKS) -> refuse_if_at_capacity -> spawn

Today the guard drops the instant the thread exits, including while the loop is
blocked in `poll`. Under parent ownership it drops only at the next iteration's
reap, so a session ending during the poll keeps its slot and can refuse a
connection master admits. `handle_accepted_connection` therefore reaps
immediately before consulting the cap, so admission always reads freshly-reaped
state. Under a forked backing that same call site is where
`reap_finished_children` belongs.

Two tests, each mutation-proven, and they discriminate differently:

  - `a_finished_worker_holds_its_slot_until_reaped` pins WHERE the slot is
    released - held by the parent after the session ends, freed by the reap.
  - `admission_reaps_before_consulting_the_connection_cap` pins the ORDERING.
    `state.served` is the discriminator: the refusal arm returns before the
    increment, and both arms return `false`, so the return value cannot tell
    them apart.

Removing the reap-before-capacity call fails only the second (`left: 0,
right: 1` - the connection was refused). Making the reap leak the slot fails
both, since both depend on the reap releasing it. Without the second test that
new call would have been an unproven line.

No behaviour change is intended: 1991/1991 daemon tests, workspace clippy
--all-features clean, whole-tree rustfmt clean.
@oferchen
oferchen enabled auto-merge (squash) September 6, 2026 19:13
@oferchen
oferchen merged commit 746a735 into master Sep 6, 2026
78 checks passed
@oferchen
oferchen deleted the fork-f2a branch September 6, 2026 19:16
oferchen added a commit that referenced this pull request Sep 6, 2026
…ssion ended (#7716)

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 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