Skip to content

fix(daemon): keep accepting after one client's session error - #7590

Merged
oferchen merged 1 commit into
masterfrom
fix/daemon-session-error-listener
Sep 1, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/daemon-session-error-listener

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner

The defect

join_worker treated any session error that was not a connection close as fatal to the accept loop. One malformed client killed the listener for everybody:

Err(DaemonError { exit_code: SocketIo, message: "failed to serve legacy handshake
127.0.0.1:54215: invalid daemon connection transition: ModuleSelect -> ModuleSelect" })

Upstream has no mechanism to port here

Upstream serves each connection in a forked child process, so a session outcome physically cannot reach the parent's accept loop. oc-rsync uses threads, so this failure mode is ours alone and the fix has to be reasoned out rather than copied. What upstream establishes is the contract, at the 3.5.0 pin:

upstream 3.5.0 what it does
socket.c:697-699 open_socket_in() returns NULL -> exit_cleanup(RERR_SOCKETIO)
socket.c:708-716 listen() fails -> exit_cleanup(RERR_SOCKETIO)
socket.c:724 while (1) - the loop body has no error exit
socket.c:738 poll(...) < 1 -> continue
socket.c:748 accept() failed (fd < 0) -> continue
socket.c:753-765 child runs fn(fd, fd), ends at _exit(ret)
socket.c:766-773 fork() failed -> log, close fd, keep looping
socket.c:676-684 sigchld_handler() reaps with waitpid(-1, NULL, WNOHANG) - NULL status pointer, so the session's outcome is discarded outright

The contract: only listener setup can end the daemon. No per-connection outcome, of any kind, is even inspected.

Error classification

Everything join_worker can observe originates in spawn_connection_worker's closure, which calls ConnectionContext::serve_session -> handle_session on the accepted client socket. There is no other producer of a WorkerResult.

class origin verdict
BrokenPipe / ConnectionReset / ConnectionAborted client left per-connection, silent (unchanged)
PROXY-header read/parse failure handle_session -> parse_proxy_header on the client socket per-connection, logged
greeting / module-select / auth / transfer I/O failure handle_legacy_session on the client socket per-connection, logged
rejected FSM transition (transition_error) handle_legacy_session, protocol state of that one session per-connection, logged
panic escaping catch_unwind one session's stack per-connection, logged (unchanged)
listener bind / listen / set_nonblocking failure bind_error, engine construction fatal, unchanged
accept-engine poll result AcceptEngine::poll fatal, unchanged

The last two rows never travel through a JoinHandle. So the join point observes zero listener-fatal classes.

The fix

join_worker, reap_finished_workers and drain_workers return () rather than Result. The invariant is stated in the type: the join path has no channel through which it could end the accept loop. check_signals_and_maintain likewise drops its Result - nothing that step does can fail the daemon.

This is not "swallow everything". Listener-fatal errors are on a different path entirely and still propagate out of serve_connections; a daemon whose listening socket is genuinely broken still exits rather than spinning. Per-connection errors are logged at error level against the peer, keeping the operator-visible text (failed to serve legacy handshake <peer>: <error>) byte-identical to what previously came back as a DaemonError, so existing log greps keep working. stream_error had no other caller and is removed.

Note the async accept path already logged-and-continued (async_listener.rs:199-205). The sync path was the outlier.

Verification

New tests/daemon_session_error_keeps_listener.rs: client one drives ModuleSelect -> ModuleSelect by repeating the @RSYNCD: banner; client two then completes a real push through core::client::run_client against the same listener. Asserted on the landed bytes, not on the absence of a crash.

Non-vacuity is asserted explicitly: the daemon must have logged exactly one failed to serve legacy handshake ... invalid daemon connection transition line, and the first client must have been dropped mid-session rather than answered. If the daemon ever starts handling a repeated banner cleanly, this test fails loudly rather than passing while proving nothing. (The first fixture attempt sent a bare @RSYNCD: 32.0, which protocol 32 refuses for the missing digest list - a clean @ERROR, no session error at all. That inert version is what these assertions catch.)

The test passes --no-detach explicitly. Without it RuntimeOptions::detach defaults to true on Unix, the daemon forks, and the parent of that fork is the test process, which exits 0 before any assertion runs - the test binary reported PASS in 0.013s with the body never executing.

Mutation proof. With the production change reverted and the test kept:

Summary [   0.538s] 1 test run: 0 passed, 1 failed, 548 skipped
  FAIL [   0.535s] bin::daemon_session_error_keeps_listener session_error_does_not_stop_the_accept_loop

second client must complete after the first client's session error; got
Err(ClientError { exit_code: SocketIo, ... "failed to read daemon greeting from
127.0.0.1:65389: Connection refused (61)" }), daemon=Err(DaemonError { exit_code:
SocketIo, ... "failed to serve legacy handshake 127.0.0.1:65390: invalid daemon
connection transition: ModuleSelect -> ModuleSelect" })

The second client gets Connection refused: the listener is gone. With the fix restored:

Summary [   0.618s] 1 test run: 1 passed, 548 skipped

Also added a unit test pinning the classification directly: reap_finished_workers drains a worklist of four differently-failed sessions and hands back nothing the accept loop could act on.

Gates

  • cargo fmt --all -- --check clean; tools/ci/check_rustfmt_all.py clean (3368 files - the changed files are include!d and invisible to plain cargo fmt).
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings clean.
  • tools/ci/citation_drift_audit.py exit 0.
  • tools/ci/check_daemon_no_detach.sh clean, pending ceiling unchanged at 77.
  • cargo nextest run -p daemon --all-features: 1897 tests run: 1897 passed (1 leaky), 6 skipped.
  • cargo nextest run -p bin --all-features -E 'binary(/daemon/)': 78 tests run: 78 passed, 1 skipped.

`join_worker` treated any session error that was not a connection close as
fatal to the accept loop, so a single malformed client took the listener down
for everybody:

    DaemonError { exit_code: SocketIo, message: "failed to serve legacy
    handshake 127.0.0.1:54215: invalid daemon connection transition:
    ModuleSelect -> ModuleSelect" }

A worker runs exactly one session against one accepted socket, so every
outcome it can carry describes that connection and nothing else. The join
point has no fatal class to report, so `join_worker` now returns nothing at
all and logs the failure against the peer instead. Failures of the listening
socket travel a different path - `bind_error` when a listener cannot be bound
or made non-blocking, and the accept engine's `poll` result - and still end
the daemon.

Normal closes (broken pipe, reset, aborted) stay silent, as before.

upstream serves each connection in a forked child, so this failure mode has no
analogue there to port: socket.c:753-765 runs the session in the child, which
ends at `_exit(ret)`, and socket.c:676-684 reaps it with
`waitpid(-1, NULL, WNOHANG)` - a NULL status pointer, so the parent discards
the outcome without inspecting it. The `while (1)` loop at socket.c:724 has no
error exit; `poll` failure (:738), `accept` failure (:748) and `fork` failure
(:766) each keep it running. Only listener setup is fatal, via
`exit_cleanup(RERR_SOCKETIO)` at socket.c:699 and socket.c:715.

The async accept path already logged and continued
(`async_listener.rs:199-205`); the sync path was the outlier.
@github-actions github-actions Bot added the bug Something isn't working label Sep 1, 2026
@oferchen
oferchen merged commit 7e2d939 into master Sep 1, 2026
72 checks passed
@oferchen
oferchen deleted the fix/daemon-session-error-listener branch September 1, 2026 13:39
oferchen added a commit that referenced this pull request Sep 2, 2026
`## [Unreleased]` last covered the #7304-#7522 era. Measured against the 60
most recently merged pull requests, 44 were absent; of the most recent 45,
43 were absent. Nothing from #7574 onward was recorded at all.

Each entry is derived from what the pull request actually landed - the merge
commit and the body - not from its title. Three cases where those differ:

  * #7616 ships far more than the doc-alias correction its body describes:
    the `upstream_version` pin moves to 3.5.0 and `--version` starts naming
    `quic`, `sd-notify`, `vmsplice` and `send_zc`.
  * #7618 also adds the pull-request rustdoc-link job its body says was
    filed separately, which is what closes the master-only-gate recurrence.
  * #7602's squash carries the closed #7597 as its first commit, so the
    directory-obstacle half is recorded under #7602.

#7587 is deliberately not recorded. Its merge commit is empty: the change
had already landed as #7563, which the section already names.

Entries extend the existing Keep a Changelog groups and the bold
sub-groupings. Clusters that are one story carry one entry with several
references: the FERROR_XFER routing and its two-phase drain (#7576, #7581),
the connection-FSM close edge and the accept loop that outlives a session
error (#7579, #7590), the three detaching daemon suites (#7578, #7588,
#7592), the io_uring and SEND_ZC work (#7593, #7600, #7610), the citation
retargets (#7601, #7603, #7604), and the accept-loop pin flake (#7615,
#7620).

Only `## [Unreleased]` is touched; every released section, including the
3.4.4 statements that were correct when written, is byte-identical.
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.
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