fix: use blocking stdin read instead of anyio.wrap_file() in stdio_server#2549
Open
celstnblacc wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: use blocking stdin read instead of anyio.wrap_file() in stdio_server#2549celstnblacc wants to merge 1 commit intomodelcontextprotocol:mainfrom
celstnblacc wants to merge 1 commit intomodelcontextprotocol:mainfrom
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.
Summary
Replaces
anyio.wrap_file()wrapping of stdin instdio_server()with a blockingrun_sync(raw_stdin.readline)loop.Problem
anyio.wrap_file()wrapssys.stdin.bufferas an async iterator. When the MCP client closes its end of stdin between connection cycles, the async iterator sees EOF and exits theasync forloop, killing the read stream and the server.This manifests as
-32000: Connection closederrors in MCP clients like Claude Code, OpenCode, and Codex CLI, requiring a client restart every time the connection resets.Fix
The default stdin path now uses
anyio.to_thread.run_sync(raw_stdin.readline)in awhile Trueloop. Blockingreadline()waits until the OS delivers more data, surviving transient client disconnects. An empty read (real process EOF) still cleanly exits the loop.The existing custom-stdin path (used by tests and when callers pass an
anyio.AsyncFile) is preserved unchanged.Changes
run_sync(readline)loop (37 insertions, 11 deletions)test_stdio_server_survives_stdin_eofregression test (46 insertions)Testing
All existing and new tests pass:
Ruff lint: clean.