fix: drain accepted JSON-RPC requests after read EOF#3026
Open
matthewchen94 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/mcp/shared/jsonrpc_dispatcher.py">
<violation number="1" location="src/mcp/shared/jsonrpc_dispatcher.py:556">
P2: Track builder-rejection response writes in the EOF drain too. Otherwise a request whose `transport_builder` raises just before EOF can have its spawned INTERNAL_ERROR response cancelled before it reaches the peer.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
| _progress_token=progress_token, | ||
| ) | ||
| scope = anyio.CancelScope() | ||
| self._active_inbound_requests += 1 |
There was a problem hiding this comment.
P2: Track builder-rejection response writes in the EOF drain too. Otherwise a request whose transport_builder raises just before EOF can have its spawned INTERNAL_ERROR response cancelled before it reaches the peer.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/mcp/shared/jsonrpc_dispatcher.py, line 556:
<comment>Track builder-rejection response writes in the EOF drain too. Otherwise a request whose `transport_builder` raises just before EOF can have its spawned INTERNAL_ERROR response cancelled before it reaches the peer.</comment>
<file context>
@@ -545,6 +553,7 @@ async def _dispatch_request(
_progress_token=progress_token,
)
scope = anyio.CancelScope()
+ self._active_inbound_requests += 1
# TODO(maxisbey): duplicate ids blind-overwrite (v1/TS parity); revisit
# rejecting with INVALID_REQUEST. Key coerced so a stringified
</file context>
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.
Fixes #2678.
This changes the JSON-RPC dispatcher shutdown path so read EOF does not immediately cancel request handlers that were already accepted. The dispatcher now:
_in_flight.This targets stdio servers driven with redirected stdin, where EOF can arrive immediately after the last JSON-RPC request is read while an async tool call is still mid-await. In that case the response should be flushed to stdout before shutdown.
Validation:
python3 -m py_compile src/mcp/shared/jsonrpc_dispatcher.py tests/shared/test_jsonrpc_dispatcher.pypassed on the patched files.