fix: handle ClosedResourceError when logging errors to disconnected clients#2177
Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
…lients When a client disconnects mid-request, _handle_message catches the resulting Exception and tries to send_log_message() back to the client. Since the session write stream is already closed, this raises ClosedResourceError (or BrokenResourceError), which is unhandled and crashes the stateless session with an ExceptionGroup. This is a different code path from PR modelcontextprotocol#1384, which fixed the message router loop. This bug is in the error recovery path: catch exception → try to log it to client → write stream closed → crash. Fix: catch ClosedResourceError and BrokenResourceError around the send_log_message call in _handle_message, since failing to notify a disconnected client is expected and harmless. Fixes modelcontextprotocol#2064
27a91e2 to
ab6a25b
Compare
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 mid-request in a stateless streamable-HTTP session,
_handle_messagecatches the resultingException(e.g.ClientDisconnect) and tries to callsession.send_log_message()to notify the client about the error. Since the session write stream is already closed at this point, this raisesClosedResourceError, which is unhandled inside the TaskGroup and crashes the entire stateless session with anExceptionGroup.Root Cause
In
lowlevel/server.py, theExceptionbranch of_handle_message(line ~418) unconditionally callssend_log_message(). This works fine when the client is still connected, but when the error is a client disconnect, the write stream is already closed, sosend_log_message→send_notification→_write_stream.send()raisesClosedResourceError.This is a different code path from what PR #1384 fixed (message router loop). This bug is in the error recovery path:
Fix
Wrap the
send_log_message()call in a try-except that catchesanyio.ClosedResourceErrorandanyio.BrokenResourceError, since failing to notify a disconnected client is expected and harmless. A warning is logged instead.Testing
Added 3 tests:
test_exception_handling_tolerates_closed_write_stream— ClosedResourceError is caughttest_exception_handling_tolerates_broken_write_stream— BrokenResourceError is caughttest_exception_handling_closed_stream_with_raise_exceptions— original exception is still re-raised whenraise_exceptions=TrueAll 9 exception handling tests pass (2 consecutive clean runs).
Related Issues
_handle_message→send_log_messagepathFixes #2064