fix: Ensure stdio server exits when stdin reaches EOF (fixes #2231)#2234
Open
goingforstudying-ctrl wants to merge 4 commits intomodelcontextprotocol:mainfrom
Open
fix: Ensure stdio server exits when stdin reaches EOF (fixes #2231)#2234goingforstudying-ctrl wants to merge 4 commits intomodelcontextprotocol:mainfrom
goingforstudying-ctrl wants to merge 4 commits intomodelcontextprotocol:mainfrom
Conversation
added 4 commits
March 6, 2026 14:37
When the parent process dies or stdin is closed, the stdio server now properly detects EOF and shuts down gracefully instead of becoming an orphan process. Changes: - Explicitly close read_stream_writer on EOF to signal shutdown - Add test coverage for EOF handling and parent death simulation Fixes modelcontextprotocol#2231
Pass mock stdin (empty BytesIO) and stdout to stdio_server() to avoid UnsupportedOperation when reading real stdin in CI.
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.
Problem
When an MCP server using
transport="stdio"has its parent process die (e.g. client crashes, is killed, or exits), the server process becomes an orphan and continues running instead of shutting down gracefully.Root Cause
The stdin reader loop ends when EOF is reached, but there was no explicit signal to the rest of the server that shutdown should occur.
Solution
read_stream_writeron EOF to signal shutdownChanges
src/mcp/server/stdio.py: Close read stream on EOFtests/test_stdio_eof.py: New test file with EOF testsTesting
test_stdio_server_exits_on_eof()- verifies server exits on stdin EOFtest_stdio_server_parent_death_simulation()- simulates parent process deathFixes #2231