fix: preserve timeout message through BaseExceptionGroup unwrapping#4713
Open
shoummu1 wants to merge 2 commits into
Open
fix: preserve timeout message through BaseExceptionGroup unwrapping#4713shoummu1 wants to merge 2 commits into
shoummu1 wants to merge 2 commits into
Conversation
…p unwrapping Signed-off-by: Shoumi <[email protected]>
…timeout ExceptionGroup fallback Signed-off-by: Shoumi <[email protected]>
63973b5 to
03755ba
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.
Bug-fix PR
📌 Summary
When a tool invocation times out via StreamableHTTP or SSE transport, the error
response returned to the client contains an empty message (
"Tool invocation failed: ").The descriptive timeout message is generated correctly but lost during exception
propagation through Python 3.11+
BaseExceptionGroupwrapping.🔗 Related Issue
Closes: #2782
🔁 Reproduction Steps
See #2781 and #2782.
tool_timeoutso the tool times out reliably."Tool invocation failed: "(empty message).🐞 Root Cause
tool_service.pyraisesToolTimeoutError("Tool invocation timed out after {N}s")inside nested
async withcontext managers (streamablehttp_client,ClientSession).During
__aexit__cleanup the MCP SDK's internalTaskGroupmay raise additionalexceptions from cancelled tasks, causing Python 3.11+ to wrap everything in a
BaseExceptionGroup.In the outer
invoke_toolhandler:except ToolTimeoutErrordoes not match aBaseExceptionGroupcontaining aToolTimeoutError.except BaseException.exceptions[0]) finds the bareasyncio.TimeoutError— which carries no message.str(asyncio.TimeoutError())→""→ the client sees an empty error message.The same silent-message loss affected the structured logger in the SSE and
StreamableHTTP inner handlers (logged
error_message: ""instead of the timeoutdescription).
💡 Fix Description
After unwrapping the
BaseExceptionGrouproot cause, add a fallback thatre-attaches the descriptive timeout message whenever the root cause is a bare
TimeoutError/asyncio.TimeoutErrorwith an empty string representation:Applied to three locations:
tool_service.pyroot_cause_message(logging)tool_service.pyroot_cause_message(logging)tool_service.pyinvoke_toolhandlererror_message(returned to client)The fallback is only triggered when the message is empty and the root cause
is a
TimeoutErrorsubclass, so normal non-timeout errors are completelyunaffected.
🧪 Verification
A regression test was added to
tests/unit/mcpgateway/services/test_tool_service.py:It constructs a nested
ExceptionGroup(ExceptionGroup(TimeoutError())), asserts the raisedToolInvocationErrorcontains"timed out after", and verifies the metrics buffer recordsthe correct non-empty
error_message.make lintmake testmake coverage📐 MCP Compliance (if relevant)
✅ Checklist
make black isort pre-commit)test_invoke_tool_timeout_message_preserved_through_exception_group)