Repository navigation
fix(daemon): shut sessions down through live streams and drop the deprecated split halves - #354
Merged
Merged
Conversation
…umber
Add two unix unit tests to the session registry for the control_shutdown
BrokenPipe flake. Each registers a session socket, then uses dup2 to close
that socket and hand its descriptor number to another connection in one
atomic step, which is the state a session is in after its socket closed
and before its guard removed the registry entry. The drain
(close_all_sessions) and the dead-client sweep (shutdown_session) must
leave the other connection open: it must still write, and its peer must
read the byte.
Both tests fail on the current registry, which records a raw fd and
shuts that number down: the other connection's write returns
Os { code: 32, kind: BrokenPipe }, the error the flaky control test
reported. This commit is red on purpose; the fix follows in this branch.
Verified with env -u RUSTUP_TOOLCHAIN cargo test -p codegraph-daemon
--lib --locked -- reused_a_retired_sessions_fd (2 failed, both
BrokenPipe), and clippy -D warnings plus cargo fmt --check are clean.
interprocess 2.4.7 deprecated Stream::split and the RecvHalf/SendHalf types in favour of sharing one stream: &Stream implements Read/Write (AsyncRead/AsyncWrite for the tokio stream), so clones of one Arc can read and write at once. The locked version is still 2.4.6; this change removes every use of the deprecated API so the bump compiles warning-free. - transport: add Shared<T>, an Arc newtype that forwards Read/Write and AsyncRead/AsyncWrite to &T, and a unix shutdown_write helper; drop the SendHalf and async RecvHalf/SendHalf re-exports. - session: the session's reader and writer are two Shared clones. The registry still records the socket's raw fd, read from the shared stream, so the sweep and drain behave exactly as before; their fix follows separately. - proxy: run_proxy keeps its own clone and half-closes the write direction through it, so WriteFd, write_raw_fd and their unsafe borrow_raw are gone. Holding the clone keeps the socket open until the half-close; the old code could reach a closed or reused number if the daemon-to-host pump ended first. One behaviour moves: tokio's write half sent SHUT_WR when it dropped, and a dropped clone does not. Every session path that drops the writer returns without awaiting again, so the socket still closes at the same point. The new test a_control_connection_reaches_eof_after_its_reply pins that: a client reads EOF after the daemon answers its control frame. It passed 21/21 before this change and 21/21 after, and it fails (WouldBlock after the 10 s read timeout) when a clone is leaked on purpose. Verified on interprocess 2.4.6: cargo clippy -p codegraph-daemon -p codegraph-rs --all-targets --locked -- -D warnings; cargo xwin clippy --target x86_64-pc-windows-msvc -p codegraph-daemon --all-targets; cargo test -p codegraph-daemon (all green except the two reused-fd tests from the previous commit, still red by design); codegraph-rs proxy_e2e, daemon_sweep, daemon_idle and daemon_handover.
The session registry recorded each session's raw socket fd, and the drain (close_all_sessions) and the dead-client sweep (shutdown_session) called shutdown(SHUT_RDWR) on that number. The number can outlive the socket: the session's stream closes before its guard removes the entry, and shutdown_session reads the number under the lock but calls shutdown outside it. In either window another connection can take the number, and the shutdown then lands on that connection. That is the control_shutdown flake: a drain in one test closes the next test's fresh control connection, which reports BrokenPipe on its write or EOF on its reply. In production the same race can cut off another client's session. The registry now stores a Weak handle to the session's Shared stream. shutdown_session upgrades it outside the lock and shuts the socket down through the live handle. A session that already dropped its stream no longer upgrades, so nothing is touched; while the upgraded handle is held the socket cannot close, so its number cannot be reused. The Weak never keeps a socket open. The last unsafe borrow_raw in the daemon goes with the raw fd, and Windows keeps its no-op shutdown_session. Tests: the two reused-fd tests from the previous commit now build the state on the new API (a real Shared stream is recorded and dropped, and another socket takes its number through fcntl_dupfd_cloexec, retried up to 20 times if another thread takes the number first) and pass. Two new positive controls check that shutting a live session down still makes its peer read EOF, through the sweep and through the drain. Mutation checks: restoring "shut the recorded number down" makes the reused-fd tests fail with BrokenPipe, and disabling the shutdown makes the EOF controls fail. The full lib suite passed 30 of 30 repeats. Verified on interprocess 2.4.6 and tokio 1.53.3: cargo test -p codegraph-daemon --locked (all green), codegraph-rs proxy_e2e, daemon_sweep, daemon_idle and daemon_handover, cargo clippy -p codegraph-daemon -p codegraph-rs --all-targets -D warnings, and cargo xwin clippy for x86_64-pc-windows-msvc.
Move the locked interprocess from 2.4.6 to 2.4.10; the Cargo.toml requirement stays "2.4". The lockfile change is the version and the checksum, and the dependency list is unchanged. 2.4.7 deprecated the split halves, which the previous commits stopped using; on the sources of 8166929 this lockfile fails clippy -D warnings with 14 deprecated errors on Linux and 9 on x86_64-pc-windows-msvc, the same sites CI reported on #345. cargo update -p interprocess --precise 2.4.10 also rewrote 12 windows-sys dependency edges of unrelated packages. A no-op cargo update -p interprocess --precise 2.4.6 rewrites the same edges, so they come from cargo 1.98 re-resolving this lockfile rather than from the bump; only the interprocess entry is kept. cargo metadata --locked accepts the result, and cargo tree --locked -i interprocess shows 2.4.10. Verified: cargo clippy -p codegraph-daemon -p codegraph-rs --all-targets --locked -- -D warnings and cargo xwin clippy --target x86_64-pc-windows-msvc -p codegraph-daemon --all-targets report no warnings (0 deprecated); cargo test -p codegraph-daemon --locked and codegraph-rs proxy_e2e, daemon_sweep, daemon_idle and daemon_handover pass.
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #354 +/- ##
==========================================
- Coverage 96.40% 96.40% -0.01%
==========================================
Files 368 368
Lines 254382 254517 +135
==========================================
+ Hits 245229 245357 +128
- Misses 9153 9160 +7
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Owner
Author
|
macOS verification (owner decision OD-C7), on GitHub
The macOS test portability issues are recorded for a separate change; they are not part of this patch. |
Merged
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.
Summary
control_shutdownflake:send the control frame: Os { code: 32, kind: BrokenPipe }attests/control_shutdown.rs:117, orEOF while parsing a valueon the reply. The session registry recorded each session's raw socket fd. The drain (close_all_sessions) and the dead-client sweep (shutdown_session) then calledshutdown(SHUT_RDWR)on that number, sometimes after it already named another connection's socket. The registry now keeps aWeakhandle to the session's stream and shuts the socket down through the upgraded, live handle.Stream::split,RecvHalfandSendHalf, deprecated since 2.4.7, with oneArc-shared stream per connection, and bump the locked interprocess from 2.4.6 to 2.4.10. Bothunsafe { BorrowedFd::borrow_raw(..) }calls in the daemon are gone.This PR does not change the public API, the wire protocol, the graph, the goldens or the extraction version.
Cargo.tomlstill requiresinterprocess = "2.4".Why
A recorded fd number can outlive its socket in three windows:
shutdown_sessionreads the number under the registry lock and callsshutdownafter releasing it.SessionGuardremoves the entry.select!drops the branch futures before the function's locals, and an rmcp session's transport lives in a spawned task.close_all_sessionswakes every session and then immediately shuts the same sessions down, so the drain always races the sessions it just woke.The
control_shutdowntests run in parallel in one process and share one fd table. One test's drain can therefore shut down another test's fresh control connection right after that connection reads the daemon hello. In a real daemon the same race lets a sweep or a drain cut off an unrelated client. With the fix, the upgradedArcis held across the syscall, so the socket cannot close and its number cannot be reused. A session that already dropped its stream no longer upgrades, and nothing is touched.Changes
transport.rs:Shared<T>(Arc<T>)forwardsRead/WriteandAsyncRead/AsyncWriteto&T, the replacement interprocess documents forsplit. It also adds the unix helpersshutdown_write(&Stream)andshutdown_both(&AsyncStream), and drops the re-exports of the deprecated halves.session.rs: the reader and the writer are twoSharedclones. The registry entry stores aWeak<AsyncStream>(record_socketreplacesset_recv_fd), andshutdown_sessionupgrades it after releasing the lock.shutdown_sessionis still a no-op on Windows.proxy.rs:run_proxykeeps its own clone and half-closes the write direction through it, soWriteFd,write_raw_fdand theirborrow_raware deleted. This also removes a latent copy of the bug. If the daemon-to-host pump ended first, the old code half-closed a number that dropping the send half had already closed.SHUT_WRwhen it was dropped, and a dropped clone does not. Every session path that drops the writer returns without another await, so the socket still closes at the same point. The new testa_control_connection_reaches_eof_after_its_replypins this.Verification
All runs use tokio 1.53.3, the locked version. The stress harness pins the test binary and 8 busy loops (
timeout --foreground 900 sh -c 'while :; do :; done') to 4 CPUs. No busy loop was left over after any run.Red evidence
Deterministic regression tests, commit fa6fae5, whose library code is identical to 8166929.
dup2points the recorded number at another socket, the state a retired session leaves behind.cargo test -p codegraph-daemon --lib --locked -- reused_a_retired_sessions_fd:The fix commit rewrites both tests to the new API, because the old form records a raw fd through
set_recv_fd, which no longer exists. The scenario and the assertions stay the same: a recorded stream is dropped, and another socket takes its number throughfcntl_dupfd_cloexec. If the new code is changed back to "shut the recorded number down when the handle no longer upgrades", both tests fail with the same BrokenPipe.The mechanism was traced with an uncommitted diagnostic patch on 8166929. The patch records
/proc/self/fd/Nat registration, compares it right before the syscall, and can pause to widen a window.All 4 natural failures carry the flake's signature, BrokenPipe at
:117; three of them are inan_unauthorized_frame_is_refused_without_shutting_the_daemon_down.Deprecation: with the 2.4.10 lockfile, main's sources fail
cargo clippy -p codegraph-daemon --all-targets --locked -- -D warningswith 14 deprecated errors. Thex86_64-pc-windows-msvcclippy run (cargo-xwin) fails with 9. These are the sites CI reported on #345.Green
upgrade()and the syscall. For each pause position, 0/20 churn runs and 0/200 suite runs failed. Natural runs: 0/19,379 (N = 3/p_pre, 23 min), with no stale or raced line.cargo test -p codegraph-daemon --locked: green, includingcontrol_shutdown,rmcp_reap,async_model, the rewritten tests and two positive controls. The controls check that a live session's peer still reads EOF after the sweep and after the drain; disabling the shutdown makes them fail.cargo test -p codegraph-rs --locked --test proxy_e2e --test daemon_sweep --test daemon_idle --test daemon_handover: green.a_control_connection_reaches_eof_after_its_reply: 21/21 before the migration and 21/21 after. It fails when a stream clone is leaked on purpose.cargo clippy -p codegraph-daemon -p codegraph-rs --all-targets --locked -- -D warningsis clean on 2.4.6 and on 2.4.10 (0 deprecated).cargo xwin clippy --target x86_64-pc-windows-msvc -p codegraph-daemon --all-targets --locked -- -D warningsis clean on both, and so iscargo fmt --all --check.make pre-cipassed on d82e297.CI Successon the exact head, and the macOS run (a throwaway workflow on branchperf/a2-macos-1, commit 73b99b1 = this head plus that workflow file; the result is posted as a comment and the branch is deleted afterwards).Notes
test(daemon)is red on purpose, andrefactor(daemon)leaves those two tests red;fix(daemon)turns them green. A squash merge leaves one commit on main.cargo update -p interprocess --precise 2.4.10also rewrote 12 windows-sys dependency edges of unrelated packages. A no-opcargo update -p interprocess --precise 2.4.6rewrites the same 12, so they come from cargo 1.98 re-resolving the lockfile, not from the bump. Only the interprocess entry (version and checksum) is kept, andcargo metadata --lockedaccepts it.shutdown_sessionstays a no-op there. The shared stream closes the way the old split did, because both halves already shared oneRawPipeStream.UnixStream::shutdown, is not part of this PR. Rerun the harness when it lands.