fix: handle cancellation race in RequestResponder.respond()#2417
Open
lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle cancellation race in RequestResponder.respond()#2417lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
When a CancelledNotification arrives after the handler completes but before respond() is called, cancel() sets _completed=True and sends an error response. The subsequent respond() call would hit an AssertionError. This change replaces the assert with a guard: if _completed is already True, respond() returns silently since the cancellation response was already sent. Fixes: modelcontextprotocol#2416 _Submitted via TTClaw bounty hunter._
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: Cancellation Race in
RequestResponder.respond()When a
CancelledNotificationarrives after the handler completes but beforerespond()is called,cancel()sets_completed=Trueand sends an error response.The subsequent
respond()call would hitAssertionError: Request already responded to.The Fix
Replace the
assert not self._completedguard with a silent early-return:This is safe because
cancel()already sent an error response ("Request cancelled"),so skipping the duplicate response is correct behavior.
Why This Is The Right Fix
cancel()is called on a different task (the cancellation notification handler)respond(), there is no async checkpointCancelledErrorcannot be raised in that gapBrokenResourceError/ClosedResourceErrorare already handled inserver.py_completedFixes #2416
Submitted via TTClaw bounty hunter.