Skip to content

fix(platform): reap a session child by pid instead of sweeping every child - #7718

Merged
oferchen merged 1 commit into
masterfrom
fix/session-fork-per-pid-reap
Sep 6, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/session-fork-per-pid-reap

Conversation

@oferchen

@oferchen oferchen commented Sep 6, 2026

Copy link
Copy Markdown
Owner

FORK-EPIC F2b, step 2 groundwork. session_fork is staged for the fork-backed session and has zero production callers; this replaces the one primitive in it that a future caller could not safely use.

The bulk reaper was a latent defect waiting for a caller

reap_finished_children swept with waitpid(-1, .., WNOHANG) and returned a count. connection.rs:314 pointed the next reader straight at it — "under a forked backing this is also where reap_finished_children belongs" — and wiring it there would have been wrong twice over.

It steals other children's statuses. The daemon forks children that are not sessions: the name converter helper, the pre-/post-transfer exec hooks, the auth helper. Those are owned by std::process::Child and reaped by 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 must 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>

Deliberately the same shape as the SessionWorker::try_reap / wait pair from #7716, so a fork backing slots 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. Under fork, SessionOutcome::Died becomes a fatal signal rather than an escaped panic; that is exactly why the two layers must not share one enum.

upstream: socket.c:676-684 sigchld_handler() does use waitpid(-1, ..) — but with 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.

Non-vacuity

a_running_child_is_not_reaped_and_a_killed_one_reports_its_signal carries two claims a single-outcome stub fails: a child that has not ended reports None, and a killed one reports Signalled rather than a clean exit. Without it, a try_reap hard-wired to Some(Exited(0)) satisfies every other assertion in the module.

Mutation-proven — 0 => Ok(None) changed to 0 => Ok(Some(ChildEnd::Exited(0))):

4 tests run: 2 passed, 2 failed
  FAIL  try_reap_reports_the_childs_exit_code
  FAIL  a_running_child_is_not_reaped_and_a_killed_one_reports_its_signal

The two that stay green are the negative controls: fork_session_splits_parent_and_child and try_reap_refuses_a_pid_that_is_not_our_child.

That last one is the whole safety argument in a test: a pid that is not ours is an error, never a fabricated outcome (ECHILD).

One deliberate loop

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.

Verification

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

  • cargo fmt -p platform -- --check clean — ⚠ my first check reported FMT=0 from a | tail pipeline; that was tail's exit code and rustfmt was actually failing. Re-run without the pipe: real rc 1, one assert_eq! reflowed, now clean.
  • clippy --workspace --all-targets --all-features --no-deps -D warnings clean
  • nextest -p platform session_fork 4/4

Scope

Still no production caller — the module stays staged. What changes is that the API it stages is now one that step 2 can safely use, and the footgun is gone rather than waiting to be picked up. The connection.rs comment is retargeted to say per-pid, one call per worker the parent owns, never a bulk sweep.

…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.
@github-actions github-actions Bot added the bug Something isn't working label Sep 6, 2026
@oferchen
oferchen merged commit 43e07be into master Sep 6, 2026
78 checks passed
@oferchen
oferchen deleted the fix/session-fork-per-pid-reap branch September 6, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant