Skip to content

fix: drain accepted JSON-RPC requests after read EOF#3026

Open
matthewchen94 wants to merge 1 commit into
modelcontextprotocol:mainfrom
matthewchen94:fix/stdio-drain-inflight-eof-2678
Open

fix: drain accepted JSON-RPC requests after read EOF#3026
matthewchen94 wants to merge 1 commit into
modelcontextprotocol:mainfrom
matthewchen94:fix/stdio-drain-inflight-eof-2678

Conversation

@matthewchen94

Copy link
Copy Markdown

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:

  • marks the connection closed and wakes outbound waiters after read EOF,
  • keeps the write stream open briefly for accepted inbound requests to finish their response writes,
  • bounds that drain window to avoid hanging shutdown,
  • tracks active inbound requests through the response write window, not just while they are present in _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.py passed on the patched files.
  • Added a dispatcher-level regression test that accepts a request, closes the read side, and asserts the response is still written.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@cubic-dev-ai cubic-dev-ai Bot Jun 30, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastMCP/stdio: in-flight tool responses dropped on stdin EOF when input is bash-redirected from a file

1 participant