fix(sdk-mcp): release pending request id when unsent send is cancelled #1562
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.43) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 359 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.352
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #1562"
general
weight: 0.35
tonydzi
Fixes #1557
Symptom & Repro
Cancelling
SdkMcpBridge.handle()while_Session.submit()is blocked inawait self._to_server.send(parsed)(for example while a server lifespan is still starting up) removes the waiting send from the AnyIO memory stream, but leaves its_PendingResponseentry permanently registered in_Session._pending. Once startup completes, reusing that JSON-RPC request ID fails withException: Request id <id> is already in flight.Root Cause
_Session.submit()registered_PendingResponsebefore awaiting_to_server.send(), and only cleared it onClosedResourceError/BrokenResourceError. As noted in #1557, a blanketexcept BaseException:cleanup is unsafe becauseanyio'sreceive_nowait()moves a waiting sender's message into the stream buffer and setssend_eventbefore returning; a rawasyncio.Task.cancel()in that scheduling gap still raisesCancelledErrorout ofsend()even though the server now owns the queued message.Fix
_InboundReceiveStream(preserving bothmcp1.xMemoryObjectReceiveStreammethods/sync-cm andmcp2.xContextReceiveStream.last_contextvia__getattr__) to track actual message consumption (_consume) and filter out aborted handoff items on bothreceive()andreceive_nowait()._send_lockin_send_tracked()across bothsubmit()and_answer_server_request(), using anO(1)FIFO tombstone queue (_InboundState.aborted) only whenrecv_countchanged while a send was in flight:- Cancelled while waiting before admission or when
recv_countdid not advance: immediately removes tracking and resolvespending(unsent) so the ID can be reused safely.- Cancelled during an ambiguous buffer-handoff bump before consumption: marks the item aborted so
_InboundReceiveStreamdrops it instead of executing it, while sweeping stale aborted entries as subsequent tracked messages/ping replies arrive.- Cancelled after consumption (
attempt.delivered == True): preserves the existing invariant that the ID remains reserved until the server responds.asyncioandtriointests/test_sdk_mcp_integration.py(barrier slow-start cancellation, delivered-request ID retention, rawasyncio.Task.cancel()buffer-handoff drop, batch-cancel idle cleanup, ping-interleaved tombstone sweep, andMemoryObjectReceiveStreamcompatibility).Verification
origin/main(69846cd) across bothasyncioandtrio.-
uv run pytest: 1769 passed, 6 skipped-
uv run ruff check src/ tests/: passed-
uv run ruff format --check src/ tests/: 65 files already formatted-
uv run mypy src/: no issues found in 27 source files