fix: catch ClosedResourceError in error handler to prevent crash on client disconnect#2160
Closed
giulio-leone wants to merge 3 commits intomodelcontextprotocol:mainfrom
Closed
Conversation
…lient disconnect When a client disconnects during request processing, _handle_message receives the exception and tries to send a log message back to the client via send_log_message(). Since the write stream is already closed, this raises ClosedResourceError (or BrokenResourceError), which is unhandled and crashes the stateless session with an ExceptionGroup. Wrap the send_log_message() call in a try/except that catches both anyio.ClosedResourceError and anyio.BrokenResourceError, since failing to notify a disconnected client is expected and harmless. This is a different code path from PR modelcontextprotocol#1384, which fixed the message router loop. This fix covers the _handle_post_request → _handle_message → send_log_message error recovery path. Fixes modelcontextprotocol#2064
45fda18 to
61e4c95
Compare
Author
|
Closing in favor of #2161 which covers the same scenario. |
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
Fixes #2064
When a client disconnects during request processing,
_handle_messagereceives the exception and tries to send a log message back viasend_log_message(). Since the write stream is already closed, this raisesClosedResourceError, which is unhandled and crashes the stateless session with anExceptionGroup.Root Cause
In
lowlevel/server.py, theExceptionbranch of_handle_message(line ~418) callssession.send_log_message()unconditionally. When the error is a client disconnect, the write stream is already closed, so the chainsend_log_message → send_notification → _write_stream.send()raisesClosedResourceError.This is a different code path from PR #1384, which fixed
ClosedResourceErrorin the message router loop. This fix covers the_handle_post_request → _handle_message → send_log_messageerror recovery path.Fix
Wrap the
send_log_message()call in a try/except that catches bothanyio.ClosedResourceErrorandanyio.BrokenResourceError, logging a warning instead of crashing. Failing to notify a disconnected client is expected and harmless.Test Results
475 server tests pass (2 consecutive clean runs).