fix: handle ClosedResourceError in _handle_message error recovery path#2072
Open
BabyChrist666 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
…ontextprotocol#2064) When a client disconnects mid-request, the exception handler in _handle_message tries to send_log_message() back to the client. Since the write stream is already closed, this raises ClosedResourceError, which crashes the stateless session with an ExceptionGroup. Wrap the send_log_message call in a try/except that catches ClosedResourceError and BrokenResourceError, since failing to notify a disconnected client is expected and harmless. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2 tasks
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
Closes #2064
When a client disconnects while a stateless streamable-HTTP server is processing a request, the error handler in
_handle_messagetries tosend_log_message()back to the client. Since the session was already terminated and the write stream closed, this raisesClosedResourceError, which is unhandled and crashes the stateless session with anExceptionGroup.This is a different code path from what PR #1384 fixed. That PR addressed
ClosedResourceErrorin the message router loop. This bug is in the error recovery path: catch exception → try to log it to client → write stream already closed → crash.Fix
Wrap the
send_log_message()call in_handle_message's exception handler with atry/exceptthat catchesanyio.ClosedResourceErrorandanyio.BrokenResourceError. Failing to notify a disconnected client is expected and harmless — we just log it at debug level and move on.This follows the same pattern already used elsewhere in the codebase (
session.py:406,streamable_http.py:587,streamable_http.py:1009, etc.).Test plan
test_lowlevel_exception_handling.py:test_exception_handling_with_disconnected_client[ClosedResourceError]test_exception_handling_with_disconnected_client[BrokenResourceError]test_exception_handling_with_disconnected_client_raise_exceptions[ClosedResourceError]test_exception_handling_with_disconnected_client_raise_exceptions[BrokenResourceError]🤖 Generated with Claude Code