fix: drain stdio responses after stdin EOF#2815
Open
fengjikui wants to merge 1 commit into
Open
Conversation
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.
Summary
This keeps the stdio server write side alive briefly after stdin EOF so responses for already-accepted requests can flush before the process exits.
The default server/session behavior is unchanged for other transports: read-stream closure still closes the write stream and cancels in-flight handlers. Only
MCPServer.run_stdio_async()opts into the bounded drain path.Root Cause
For file-redirected stdio input, the server can read and accept
tools/callrequests, then immediately hit stdin EOF while those handlers are still mid-await.Before this change:
BaseSession._receive_loop()wrapped the read and write streams in oneasync with, so read EOF closed the write stream.Server.run()then unconditionally cancelled the handler task group.Change
Server.run()drain option that waits for in-flight requests to complete, bounded by a timeout, then falls back to cancellation.Validation
id=0, missingid=1andid=2despite bothCallToolRequesthandlers being entered.id=[0, 1, 2]and both tool response texts.uv run --frozen ruff format src/mcp/shared/session.py src/mcp/server/session.py src/mcp/server/lowlevel/server.py src/mcp/server/mcpserver/server.py tests/server/test_stdio.py tests/server/test_cancel_handling.pyuv run --frozen ruff check src/mcp/shared/session.py src/mcp/server/session.py src/mcp/server/lowlevel/server.py src/mcp/server/mcpserver/server.py tests/server/test_stdio.py tests/server/test_cancel_handling.pyuv run --frozen pytest tests/server/test_stdio.py tests/server/test_cancel_handling.py tests/server/test_lowlevel_exception_handling.py -q-> 13 passeduv run --frozen pyright src/mcp/shared/session.py src/mcp/server/session.py src/mcp/server/lowlevel/server.py src/mcp/server/mcpserver/server.py tests/server/test_stdio.py tests/server/test_cancel_handling.py-> 0 errorsUV_FROZEN=1 uv run --frozen strict-no-cover-> passedNote: I also attempted
./scripts/test; local HTTP/SSE/WebSocket tests were affected by my machine's proxy environment (socks5://127.0.0.1:7890), so I did not count that as a clean validation signal.AI Assistance Disclosure
I used AI assistance to help trace the shutdown path and draft the patch. I reviewed the changed code and ran the validation above locally.