fix: catch ClosedResourceError in _handle_message error recovery path#2242
Open
herakles-dev wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
When a client disconnects while a stateless streamable-HTTP server is reading the request body, 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 unhandled ExceptionGroup. Wrap the send_log_message() call in a try/except for ClosedResourceError and BrokenResourceError, matching the pattern already used throughout streamable_http.py (lines 589, 915, 1011, 1020). Failing to notify a disconnected client is expected and harmless. Github-Issue: modelcontextprotocol#2064 Reported-by: dannygoldstein
Single-line the debug message as ruff format prefers. Github-Issue: modelcontextprotocol#2064
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
When a client disconnects while a stateless streamable-HTTP server is reading the request body,
_handle_messagecatches the stream exception but then tries tosend_log_message()back to the already-disconnected client. Since the write stream is closed, this raisesClosedResourceError, which propagates unhandled and crashes the stateless session with anExceptionGroup.This is a different code path from what PR #1384 fixed (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_messagewith a try/except foranyio.ClosedResourceErrorandanyio.BrokenResourceError. This matches the existing pattern already used throughoutstreamable_http.py(lines 589, 915, 1011, 1020). Failing to notify a disconnected client is expected and harmless.Changes
src/mcp/server/lowlevel/server.py— catchClosedResourceError/BrokenResourceErroraroundsend_log_message()in theExceptionbranch of_handle_message, log at debug level instead of crashingtests/server/test_lowlevel_exception_handling.py— two regression tests:test_exception_handling_tolerates_closed_write_stream— parametrized over both error types, verifies no crash whensend_log_messageraisestest_exception_handling_closed_stream_still_reraises_when_requested— verifiesraise_exceptions=Truestill propagates the original error even when the write stream is closedTest plan
ClosedResourceErrorandBrokenResourceErrorscenariosraise_exceptions=Truestill correctly re-raises the original error after the log attempt failsFixes #2064