fix(core): register signal handler before startup work and handshake - #328
Merged
Conversation
serve_with previously ran config validation, workspace-root heuristics, and spawn_lsp_servers_background before run_stdio ever registered a SIGTERM/SIGINT handler, and run_stdio itself only registered one after mcp_server.serve(..)'s MCP initialize handshake resolved. rmcp's serve(..) awaits the client's first message internally, so a signal arriving at any point up to and including that wait fell through to the OS's default disposition: immediate termination, skipping Translator::shutdown_servers and risking an orphaned LSP child process mid-spawn. ShutdownSignal is now constructed once, as the first statement in serve_with, before any startup work, and moved by value into whichever transport runs; run_stdio races it against the handshake itself, then reuses the same instance in its existing post-handshake select!. Also fixes a related gap found while designing the reused handle: SIGINT was re-registered via tokio::signal::ctrl_c() on every wait (a fresh one-shot listener each time), silently losing a signal delivered while a different select! branch was being polled. ShutdownSignal now holds a persistent listener per signal kind for its entire lifetime instead.
bug-ops
force-pushed
the
fix/318-sigterm-handler-race
branch
from
August 5, 2026 02:58
ba7638c to
f463712
Compare
bug-ops
enabled auto-merge (squash)
August 5, 2026 02:58
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
run_stdioregistered itsSIGTERM/SIGINThandler only aftermcp_server.serve(..)'s MCPinitializehandshake resolved, andserve_withran config validation, workspace-root heuristics, andspawn_lsp_servers_backgroundbeforerun_stdioever got a chance to register one. A signal delivered anywhere in that window fell through to the OS's default disposition (immediate kill), skippingTranslator::shutdown_serversand risking an orphaned LSP child process mid-spawn.ShutdownSignalis now constructed once, as the first statement inserve_with, before any startup work runs, and moved by value into whichever transport runs;run_stdioraces it against the handshake itself, then reuses the same instance in its existing post-handshakeselect!.SIGINTwas previously re-registered via a fresh one-shottokio::signal::ctrl_c()on every wait, which could silently lose a signal delivered while a differentselect!branch was being polled.ShutdownSignalnow holds a persistent listener per signal kind for its entire lifetime.Fixes #318.
Test plan
cargo +nightly fmt --all -- --checkcargo clippy --all-targets --all-features --workspace -- -D warningscargo nextest run --workspace --all-features --lib --bins(643 passed)RUSTFLAGS="-D warnings" RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps --workspace --all-featurescargo test --doc --workspace --all-featurestest_e2e_sigterm_exits_promptly_during_handshake_wait, targeting the actual pre-fix window (SIGTERM sent beforeinitialize()is ever called): 5/5 failures against pre-fix code, 10/10 passes against the fixtest_e2e_sigterm_exits_promptly_while_client_stdin_open(mcpls process does not exit on SIGTERM/SIGINT while a stdio client is still connected #308 regression) still passes with its artificial delay removed