fix(session): log exceptions in default message_handler instead of silently swallowing#2374
Open
Aboudjem wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
…lently swallowing The default _default_message_handler in ClientSession silently discarded all messages including exceptions, making transport errors (e.g. SSE read timeouts) impossible to diagnose. Callers had no indication that an error occurred, leading to silent hangs that were extremely difficult to debug. The handler now logs exceptions at WARNING level with full traceback via exc_info. This provides observability while preserving backward compatibility — callers who want to re-raise exceptions or implement custom error handling can still pass their own message_handler. Github-Issue: modelcontextprotocol#1401 Reported-by: Unshure
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 #1401.
The default
_default_message_handlerinClientSessionsilently discarded all messages — including exceptions. When a transport error (e.g. SSE read timeout) was delivered through the read stream, the handler did nothing butawait anyio.lowlevel.checkpoint(), making it impossible to diagnose why requests were hanging.This change adds a
logger.warning()call with full exception traceback (exc_info) when the default handler receives anException. This provides immediate observability for transport errors without breaking backward compatibility — callers who want custom behavior (re-raising, suppression, etc.) can still pass their ownmessage_handler.What changed
_default_message_handlernow logs a warning with the exception traceback when it receives anException, instead of silently discarding itWhy not re-raise?
Re-raising in the default handler would terminate the receive loop and send
CONNECTION_CLOSEDerrors to all pending requests. While this prevents hangs, it also affects non-critical exceptions routed through_handle_incoming(e.g. responses with unknown IDs). Logging provides observability without this side effect. Transport-level error propagation to response streams is addressed separately in #2122.Test plan
test_default_message_handler_logs_exceptions— verifies the exception is logged at WARNING level with correct exc_infotest_custom_message_handler_can_suppress_exceptions— verifies custom handlers can still suppress exceptionsruff checkcleanpyrightclean