Skip to content

fix(stdio): drain responses after stdin EOF - #2680

Open
Epochex wants to merge 9 commits into
modelcontextprotocol:mainfrom
Epochex:fix/2678-stdio-drain-writes
Open

Epochex wants to merge 9 commits into
modelcontextprotocol:mainfrom
Epochex:fix/2678-stdio-drain-writes

Conversation

@Epochex

@Epochex Epochex commented May 25, 2026

Copy link
Copy Markdown

Fixes #2678.

When stdin hits EOF (e.g. python server.py < payload.jsonl > response.jsonl), the client is done sending requests but is still reading stdout for results. On main, BaseSession._receive_loop() closes the write stream as soon as the read stream ends, and Server.run() cancels the handler task group on transport close. That combination can cancel in-flight tool handlers before they respond, dropping the last ools/call responses.

This change lets ServerSession keep the write stream open when the read side closes, so Server.run() can drain already-started handlers and flush their responses before shutdown.

Tests:

  • uv run --frozen pytest tests/server/test_cancel_handling.py tests/server/test_stdio.py

Copy link
Copy Markdown

Would POSIX pipe backpressure/disconnect tests belong in this PR, or should they remain a #3674 follow-up? This scenario goes beyond the stated assumption that the client continues reading stdout.

At head 7d386fb72ebbb1e3b457fe50959119dd08ee5d00, two narrow diagnostics observed no exit within a five-second watchdog when stdout remained unread after stdin EOF, and exit 1 with BrokenPipeError when stdout was closed instead. These are observations, not confirmation of a PR bug: the environment used AnyIO 4.14.2 rather than the locked 4.10.0, and neither the full PR nor its locked environment was validated.

Could you clarify the intended disconnect semantics and whether transport-write shutdown shares the handler-drain bound? A separate POSIX candidate uses a one-second writer drain, but that policy has not been agreed with maintainers. The shared Windows hangs remain unresolved.

I am not assigned to #3674 and have not submitted these tests. AI assistance was used for the review, diagnostics, and this question.

This branch has not been deployed

No deployments
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

2 participants