[v1.x] fix: handle ClosedResourceError when transport closes mid-request#2334
Open
owendevereaux wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Open
Conversation
Backports fixes from modelcontextprotocol#2306 to v1.x to address issue modelcontextprotocol#2328. When the transport closes while handlers are processing requests (e.g., stdin EOF during a long-running tool call), the server could crash with ClosedResourceError when trying to send a response through the already-closed write stream. This fix: 1. Wraps the message loop in a try/finally that cancels in-flight handlers when the transport closes, preventing them from attempting to respond 2. Catches BrokenResourceError and ClosedResourceError when calling message.respond() and logs instead of crashing 3. Properly re-raises transport-close cancellation to let the task group handle it (vs client-initiated cancellation which already sent a response) 4. Uses list() snapshot when iterating _response_streams in the finally block to avoid 'dictionary changed size during iteration' errors Fixes modelcontextprotocol#2328
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
Backports fixes from #2306 to v1.x to address issue #2328.
When the transport closes while handlers are processing requests (e.g., stdin EOF during a long-running tool call, or a malicious client sending invalid UTF-8 bytes that crashes the transport), the server could crash with
ClosedResourceErrorwhen trying to send a response through the already-closed write stream.Changes
list()snapshot when iterating_response_streamsto avoid 'dictionary changed size during iteration' errorsTesting
Added two new test cases that reproduce the crash scenarios:
test_server_cancels_in_flight_handlers_on_transport_closetest_server_handles_transport_close_with_pending_server_to_client_requestsAll existing tests continue to pass.
Fixes #2328