Skip to content

fix: Ensure stdio server exits when stdin reaches EOF (fixes #2231)#2234

Open
goingforstudying-ctrl wants to merge 4 commits intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:fix/stdio-eof-shutdown
Open

fix: Ensure stdio server exits when stdin reaches EOF (fixes #2231)#2234
goingforstudying-ctrl wants to merge 4 commits intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:fix/stdio-eof-shutdown

Conversation

@goingforstudying-ctrl
Copy link

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

  • Explicitly close read_stream_writer on EOF to signal shutdown
  • Add test coverage for EOF handling
  • Add test for parent death simulation scenario

Changes

  • src/mcp/server/stdio.py: Close read stream on EOF
  • tests/test_stdio_eof.py: New test file with EOF tests

Testing

  • Added test_stdio_server_exits_on_eof() - verifies server exits on stdin EOF
  • Added test_stdio_server_parent_death_simulation() - simulates parent process death

Fixes #2231

Developer 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.
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.

Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown)

1 participant