fix: catch ClosedResourceError in _handle_message error recovery path#2174
Closed
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Closed
fix: catch ClosedResourceError in _handle_message error recovery path#2174giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
When a client disconnects during a request, _handle_message receives the exception and tries to send_log_message() back to notify the client. Since the write stream is already closed, this raises ClosedResourceError (or BrokenResourceError), which is unhandled in the TaskGroup and crashes the stateless session with an ExceptionGroup. Wrap the send_log_message() call in _handle_message's Exception branch with a try/except for ClosedResourceError and BrokenResourceError, since failing to notify a disconnected client is expected and harmless. Fixes modelcontextprotocol#2064
fc4b36d to
6e63d96
Compare
Author
|
Closing in favor of #2177 which handles the same error path. |
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.
Problem
When a client disconnects during a stateless streamable-HTTP request,
_handle_messagecatches theClientDisconnectexception but then tries tosend_log_message()back to the client. Since the session was already terminated and the write stream closed, this raisesClosedResourceError, which is unhandled in the TaskGroup 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:Root Cause
In
lowlevel/server.py,_handle_messagehas a catch-all exception handler (line ~418) that callssession.send_log_message()to notify the client about the error. When the error is a client disconnect, the write stream is already closed, sosend_log_message→send_notification→_write_stream.send()raisesClosedResourceError.Fix
Wrap the
send_log_message()call with a try/except that catchesanyio.ClosedResourceErrorandanyio.BrokenResourceError, since failing to notify a disconnected client is expected and harmless.Tests
Added 3 tests to
test_lowlevel_exception_handling.py:test_exception_handling_survives_closed_write_stream—ClosedResourceErrorfromsend_log_messageis caughttest_exception_handling_survives_broken_write_stream—BrokenResourceErroris caughttest_exception_handling_raises_when_configured_despite_closed_stream— withraise_exceptions=True, the original error is still re-raisedValidation
Fixes #2064