Update session.py#2149
Open
DenisStefanAndrei wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
Author
|
@maxisbey thoughts on this? |
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.
Fix: propagate CancelScope.exit return value in RequestResponder.exit
RequestResponder.__exit__callsself._cancel_scope.__exit__()but did notreturn its result. This prevented the cancel scope from suppressing
CancelledErrorwhen a request is cancelled vianotifications/cancelled.Motivation and Context
When a client sends a
notifications/cancellednotification (e.g. after arequest timeout), the server calls
RequestResponder.cancel()which cancels theinner
CancelScope. This raisesCancelledErrorin the handler task, which isa
BaseExceptionand not caught by theexcept Exceptionhandlers in_handle_request.The
CancelScope.__exit__correctly identifies this as its own cancellation andreturns
Trueto suppress it. However,RequestResponder.__exit__discardedthat return value, implicitly returning
None. Python treats a falsy return from__exit__as "do not suppress", so theCancelledErrorleaked out of thewith responder:block in_handle_message, crashed theserver.run()taskgroup, and caused the session to tear down.
After teardown, the
stdin_readertask instdio_serverremains alive (blockedwaiting for input on stdin). On the next incoming request, it attempts to write
to the now-closed
read_stream, raisingBrokenResourceErrorand crashing theserver process.
This bug is masked on Python 3.11 because asyncio's looser cancellation counter
tracking tolerates an unsuppressed
CancelledErroraftertask.uncancel()hasbeen called. Python 3.12 tightened this behavior, exposing the issue.
How Has This Been Tested?
Reproduced by connecting a Node.js MCP client (stdio transport) to a Python
FastMCP server. Triggering a request timeout causes the client SDK to send
notifications/cancelled. Without the fix, the next request to the servercrashes the process. With the fix, the server correctly suppresses the
CancelledErrorand continues serving subsequent requests.Breaking Changes
None. This is a bugfix that restores the intended behavior of request
cancellation.
Types of changes
Checklist
Additional context
The fix is a single
returnkeyword added toRequestResponder.__exit__insrc/mcp/shared/session.py:Before (broken):
self._cancel_scope.exit(exc_type, exc_val, exc_tb)
After (fixed):
return self._cancel_scope.exit(exc_type, exc_val, exc_tb)