Request timeout hangs indefinitely on stalled transport while sending cancellation

perfloop-oss/rust-sdk · Missing Deadline

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

Verdict

VERIFIED · settled 2026-12-03 · pull request opened as modelcontextprotocol/rust-sdk#1531

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

Hypothesis

When an outbound MCP request exceeds its configured timeout or maximum total timeout, the waiter attempts to send a cancellation notification to the remote peer and waits indefinitely for that write to finish before returning the timeout error. If the transport write side is stalled or backpressured—a primary cause of request timeouts when a peer hangs—the request never returns a timeout error and hangs indefinitely. Reach is opt_in because callers must configure a timeout via `PeerRequestOptions::with_timeout` or `with_max_total_timeout`; as an engineering prior grounded in MCP client timeout usage and `AsyncRwTransport`'s shared write mutex, any client relying on request timeouts to detect hung stdio/pipe/TCP peers or wedged transport workers will lose its timeout guarantee whenever the peer stops draining its input buffer (production prevalence unmeasured; depends on transport backpressure frequency).

In `RequestHandle::await_response` (`crates/rmcp/src/service.rs:626-636`) and `RequestHandle::await_response_with_progress_timeout` (`crates/rmcp/src/service.rs:692-711`), expiration invokes `RequestHandle::send_timeout_cancel_notification` (`crates/rmcp/src/service.rs:660-672`) before returning `Err(ServiceError::Timeout { timeout })`. That helper calls `let _ = self.peer.send_notification(notification.into()).await;` (line 671), discarding the result yet waiting without a deadline on both `peer.tx.send(...)` and the oneshot response from `serve_inner`'s `send_task_set` (`Peer::send_notification`, lines 897-907). In `AsyncRwTransport::send` (`crates/rmcp/src/transport/async_rw.rs:234-250`), sends serialize on `self.write.lock().await` and `FramedWrite::send(item).await`, so a stalled write blocks the cancel notification indefinitely. By contrast, `Peer::try_cancel_request` (`crates/rmcp/src/service.rs:1046-1061`) already performs non-blocking best-effort cancellation for dropped requests and subscriptions.

No open upstream PR touching `crates/rmcp/src/service.rs` (#1489, #1479, #1473, #1419, #1369) modifies `send_timeout_cancel_notification`. A provisional reproducible check is a Tokio test using a duplex or custom `Transport` whose `send` blocks after accepting a request (or fills a tiny duplex buffer without reading), calling `send_request_with_option` with a 50ms timeout and asserting that `await_response()` resolves to `Err(ServiceError::Timeout { .. })` within 200ms rather than hanging past an outer deadline. This claim is source-verified only; the Case should confirm the hang on both `AsyncRwTransport` backpressure and a blocking `Transport::send` mock, while verifying that normal non-stalled peers still receive the cancellation notification and that `local_responder_pool` is cleaned up.

Change to test: Avoid unboundedly awaiting transport write completion when sending a timeout cancellation notification, either by using non-blocking best-effort queueing or bounding the cancel send so RequestHandle::await_response returns ServiceError::Timeout promptly.

Where it lives

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

Evidence

The check fails before this change and passes with it: `For requests with a 50 ms idle or maximum-total timeout, including progress-reset mode, RequestHandle::await_response returns the configured ServiceError::Timeout within a five-second outer deadline even when Transport::send stalls on cancellation.`

The check fails before this change and passes with it: `For requests with a 50 ms idle or maximum-total timeout, including progress-reset mode, RequestHandle::await_response returns the configured ServiceError::Timeout within a five-second outer deadline when AsyncRwTransport cancellation is backpressured, and a complete cancellation frame is sent after the peer resumes reading.`

The check fails before this change and passes with it: `In a current-thread Tokio paused-time run of 1056 sequential server pings with 20 ms request timeouts on one peer whose notification sends never complete, every response wait returns Timeout within five seconds of virtual time and exactly 1024 timeout cancellation sends are started.`

The check fails before this change and passes with it: `When a subscription response sender closes, RequestHandle::await_response returns TransportClosed and removes the subscription and progress-watcher registrations for untimed, idle-timeout, progress-reset and maximum-total-timeout waits.`

The check fails before this change and passes with it: `On a current-thread Tokio paused-time runtime, eight concurrent requests over a writable duplex with the same 50 ms idle or maximum-total timeout, including progress-reset mode, each return the configured Timeout and each enqueue a cancellation notification received by the peer within a five-second virtual-time outer deadline.`

The check fails before this change and passes with it: `On a modern direct client with no other pending requests and never-completing cancellation writes, after each 50 ms idle or maximum-total timeout, including progress-reset mode, an unassociated roots/list request is rejected with INVALID_PARAMS rather than counting the timed-out request as still pending.`

The check fails before this change and passes with it: `On a current-thread Tokio paused-time runtime, after 1024 sequential client request timeouts whose cancellation writes never complete, dropping three pending call_tool futures still sends a cancellation notification for each of those three requests independently of the timeout cancellation limit.`

The check fails before this change and passes with it: `A progress-reset request with a 5 ms timeout whose outbound queue capacity is fully reserved returns Timeout within a 200 ms outer deadline and removes its progress timeout watcher without waiting for queue capacity.`

The check fails before this change and passes with it: `On a current-thread Tokio paused-time runtime, retargeting a timed request handle's public peer and id to another direct peer and request ID 9000 preserves the other live ping response waiter, both with the original receivers and with the two handles' response receivers swapped.`

Untimed request queueing · 9 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 7 8 +14.3% (+1) +1 to +1 loss ≤ 0.144 × |baseline median| PASSED
allocated_bytes/op 1291 1339 +3.7% (+48) +48 to +48 loss ≤ 0.038 × |baseline median| PASSED

Queued request batch · 11 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 7.031 8.031 +14.2% (+1) +1 to +1 loss ≤ 0.143 × |baseline median| PASSED
allocated_bytes/op 1685 1741 +3.3% (+56) +56 to +56 loss ≤ 0.034 × |baseline median| PASSED

Completed duplex ping · 12 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 117 118 +0.9% (+1) +1 to +1 loss ≤ 0.0087 × |baseline median| PASSED
allocated_bytes/op 15499 15677 +1.1% (+178) +178 to +178 loss ≤ 0.0117 × |baseline median| PASSED

Progress-enabled duplex ping · 13 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 120 121 +0.8% (+1) +1 to +1 loss ≤ 0.0085 × |baseline median| PASSED
allocated_bytes/op 16155 16333 +1.1% (+178) +178 to +178 loss ≤ 0.0112 × |baseline median| PASSED

Duplex tool call · 13 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 210 211 +0.5% (+1) +1 to +1 loss ≤ 0.0049 × |baseline median| PASSED
allocated_bytes/op 71226 71404 +0.2% (+178) +178 to +178 loss ≤ 0.0026 × |baseline median| PASSED

Connect ping and shutdown · 14 sample pairs

metric baseline candidate paired median change confidence range required result
allocations/op 294 295 +0.3% (+1) +1 to +1 loss ≤ 0.0035 × |baseline median| PASSED
allocated_bytes/op 123875 124707 +0.7% (+832) +832 to +832 loss ≤ 0.0069 × |baseline median| PASSED

Checks: 8 of 8 passed. Verification: no defect found.

Timeline