Skip to content

fix(daemon): make the connection close edge total - #7579

Merged
oferchen merged 1 commit into
masterfrom
refactor/daemon-fsm-total-close
Sep 1, 2026
Merged

oferchen merged 1 commit into
masterfrom
refactor/daemon-fsm-total-close

Conversation

@oferchen

@oferchen oferchen commented Sep 1, 2026

Copy link
Copy Markdown
Owner

What

The daemon's ConnectionState FSM validated each transition and then discarded the result at four of its ten production call sites. This makes the close edge total so there is nothing left to discard, and fixes the one peer-reachable failure the audit uncovered.

Call-site inventory (10 production sites)

Site Edge Before
session_runtime.rs:331 -> Closing (malformed greeting refusal) discarded (let _ =)
session_runtime.rs:369 -> Closing (client @RSYNCD: EXIT) discarded (let _ =)
session_runtime.rs:415 -> Closing (after #list) error propagated, new state dropped (_ = ...?)
session_runtime.rs:430 -> Closing (unknown command) error propagated, new state dropped (_ = ...?)
session_runtime.rs:356 Greeting -> ModuleSelect propagated
request.rs:560 -> Authenticating propagated
request.rs:578 -> Closing (unsupported auth digest) propagated
request.rs:607 -> Closing (auth failure) propagated
orchestration.rs:774 -> Transferring propagated
orchestration.rs:971 Transferring -> Closing propagated

Every discarded site targeted Closing. Closing was reachable from every non-terminal state, and none of these paths could already be closing, so the discarded Result could never have been Err. Discarding was harmless and the check was inert - the guard existed and could not run.

Change

Separate the two kinds of edge instead of expressing one through the other:

  • transition now covers only the handshake progression - Greeting -> ModuleSelect -> Authenticating -> Transferring - and rejects Closing as a target. Every remaining call site is a genuine ordering constraint whose error is propagated.
  • close() is the teardown edge. It is total from every state, returns ConnectionState rather than Result, and is a no-op on an already-closing connection, matching upstream's deliberately re-entrant _exit_cleanup() (cleanup.c:103).
  • In handle_legacy_session the four teardown points collapse into one end_session, which forwards the session exit code and asserts the handler had not already closed.

The bug this exposed

session_runtime.rs:356 is the only edge a peer can aim at the FSM. A second @RSYNCD: banner made the handler take Greeting -> ModuleSelect twice; the ordering check rejected it and the resulting io::Error is not survivable - join_worker (server_runtime/workers.rs) treats any session error that is not a connection close as fatal to the accept loop. Two greeting lines ended the daemon:

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

Upstream reads the version line exactly once, in exchange_protocols(), and takes the very next read_line_old() as the request whatever it contains (clientserver.c:1534-1538). A repeated banner is therefore a module name and draws @ERROR: Unknown module (:1563-1567). The version arm is now guarded on negotiated_protocol.is_none() and a later banner falls through to the request, which fixes both the refusal shape and the listener teardown.

Tests

crates/daemon/tests/connection_lifecycle_fsm.rs::lifecycle_repeated_version_line_is_a_module_name drives a real daemon, sends two banners and a #list, and asserts the upstream refusal, that the #list is never served, and that the accept loop survives.

Unit level: transition_never_targets_closing and close_is_total_from_every_state pin the split; exhaustive_transition_matrix and connection_state_is_sole_lifecycle_authority are updated to the progression-only table.

Mutation proof

Mutation Result
Drop the negotiated_protocol.is_none() guard lifecycle_repeated_version_line_is_a_module_name FAILS (got: "" - the daemon died); other 7 tests in the binary stay green
close() returns self instead of Closing 11 unit tests FAIL, close_is_total_from_every_state among them; all 8 protocol-level lifecycle tests stay green, confirming the FSM writes are unobservable on the wire
Restore Closing to valid_transitions(Greeting) transition_never_targets_closing, exhaustive_transition_matrix, valid_transitions_greeting FAIL; other 60 stay green

Verification

  • cargo nextest run -p daemon --all-features - 1894 passed, 6 skipped
  • cargo nextest run -p bin --all-features -E 'binary(/daemon/)' - 77 passed
  • cargo test -p daemon --doc - 10 passed
  • cargo fmt --all -- --check and rustfmt --check directly on all six touched files - clean
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings - clean

Follow-ups, not fixed here

  • join_worker making any non-connection-close session error fatal to the listener is a general availability hazard well beyond the FSM; it deserves its own change and tests.
  • handle_unknown_module and handle_module_denied return before any teardown edge is taken, so those paths never record Closing. Harmless today because nothing reads the state, but it is a gap in the model.
  • The pre-request read loop accepts OPTION --xxx lines that upstream's start_daemon never reads, an oc-only extension to the handshake.

The `ConnectionState` FSM validated its transitions and then threw the
result away at four of ten call sites: two `let _ = ...transition(...)`
and two `_ = ...transition(...)?` that dropped the new state. Every one
of those sites targeted `Closing`, which is reachable from every state,
so the `Result` they discarded could never have been `Err`.

Split the two kinds of edge instead of papering over one with the other.
`transition` now covers only the handshake progression
(Greeting -> ModuleSelect -> Authenticating -> Transferring) and rejects
`Closing` outright, so every remaining call site is a real ordering
constraint whose error is propagated. Teardown becomes `close()`, which
is total and returns the state directly - there is nothing left to
discard. In the session handler the four teardown points collapse into
one `end_session`, which forwards the exit code and asserts the session
was not already closed.

Closing the loop this way exposed the FSM's one peer-driven edge. A
second `@RSYNCD:` banner made the handler take Greeting -> ModuleSelect
twice, which the ordering check rejects; that error is not survivable,
because `join_worker` treats any session error that is not a connection
close as fatal to the listener, so two greeting lines ended the daemon.
Upstream reads the version line exactly once in `exchange_protocols()`
and takes the very next line as the request whatever it contains
(clientserver.c:1534-1538), so a repeated banner is a module name and
draws `@ERROR: Unknown module` (:1563-1567). Match that: the version arm
is guarded on `negotiated_protocol.is_none()` and a later banner falls
through to the request.
@github-actions github-actions Bot added the bug Something isn't working label Sep 1, 2026
@oferchen
oferchen merged commit 34b49e9 into master Sep 1, 2026
72 checks passed
@oferchen
oferchen deleted the refactor/daemon-fsm-total-close branch September 1, 2026 11:09
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.
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