In-flight handlers deadlock during EOF drain when using peer messaging

perfloop-oss/rust-sdk · Cyclic wait

https://perfloop.ai/t/oss/case_aep2wk4xh3

Verdict

VERIFIED · settled 2026-11-30 · merged as modelcontextprotocol/rust-sdk#1524

What happened: The check fails before this change and passes with it.

Hypothesis

When a transport's input stream closes or the service is cancelled while request handlers are still running, the service loop stops servicing outbound peer messages before waiting up to 5 seconds for those handlers to finish. Any handler that sends a progress or log notification or awaits an outbound peer request during that window deadlocks against the drain phase, stalls shutdown for the full drain timeout, and loses its response. Reach is default for stdio and pipe-based MCP servers where clients close stdin after sending a request, and common_config for any server whose handlers emit notifications during graceful shutdown; as an engineering prior grounded in the repository's existing EOF drain tests and standard MCP tool patterns (progress and logging notifications), any one-shot stdio tool call that emits a notification while stdin closes will hit this 5-second hang and silently drop its reply (unmeasured in production; depends on how often peers half-close input while handlers emit notifications).

In `serve_inner` (`crates/rmcp/src/service.rs:1832-1878`), breaking out of the main loop on `QuitReason::Closed` (5s drain) or `QuitReason::Cancelled` (2s drain) drops `sink_proxy_tx`, drains `response_send_tasks`, and loops on `sink_proxy_rx.recv().await`. However, `peer_rx` remains open and unpolled on `serve_inner`'s stack, `send_task_set` is never polled for `SendTaskResult::Notification`, and `local_responder_pool` is never cleared on EOF. Because `Peer::send_notification` (`crates/rmcp/src/service.rs:897-907`) pushes `PeerSinkMessage::Notification` onto `peer.tx` and awaits its oneshot `responder`, a handler calling `peer.send_notification` (or whose already-popped notification is still in `send_task_set`) blocks indefinitely until the drain timer expires and `serve_inner` drops `peer_rx` and `send_task_set`. Meanwhile, `sink_proxy_rx.recv().await` waits for that handler to send its response or drop its `sink_proxy_tx` clone, creating a cyclic wait that expires the drain timeout and drops the handler's response before closing the transport.

No open upstream PR on `modelcontextprotocol/rust-sdk` touching `crates/rmcp/src/service.rs` (#1489, #1479, #1473, #1419, #1369) addresses `serve_inner` drain handling. A provisional reproducible check is a paused-time Tokio integration test (modeled on `crates/rmcp/tests/test_inflight_response_drain.rs`) that starts `serve_directly` over duplex streams, sends a `tools/call` whose handler emits a notification (or sends a `ping` request) after the client closes its write half, and asserts that the client receives the JSON-RPC response in well under 1 second rather than timing out at 5 seconds with EOF. This claim is currently source-verified only; the Case should confirm it with that test and verify whether outbound notifications during EOF drain should be flushed to the still-open output stream or failed promptly.

Change to test: During the shutdown drain in serve_inner, keep servicing outbound notifications and send tasks until in-flight handlers finish, and fail pending outbound peer requests promptly on input EOF so handlers do not deadlock against the drain loop.

Where it lives

perfloop-oss/rust-sdk · crates/rmcp/src/service.rs

Evidence

The check fails before this change and passes with it: `In the eight controlled duplex schedules, rmcp server ping handlers finish successful replies during EOF or cancellation drain within one second of paused Tokio time: queued or in-flight progress notifications are delivered and queued or pending outbound peer ping requests fail with TransportClosed when no further input is read.`

The check fails before this change and passes with it: `For rmcp server ping handling on a duplex transport with output kept blocked, shutdown waits for its drain budget and returns Closed within six seconds after EOF or Cancelled within three seconds after cancellation, under paused Tokio time, for both an unfinished response send and an unfinished progress-notification send.`

Timeline