You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4611fa7
Browse filesBrowse the repository at this point in the historyBrowse files
stdio: answer every request read before stdin EOF, stop at once when stdout closes
A stdio client stops the server by closing its stdin, and keeps reading
stdout meanwhile (spec lifecycle, shutdown), so every request the server
had already read is still owed an answer. The dispatcher cancelled all
in-flight handlers the moment its read stream ended, which lost results
that had already been computed and were queued behind the stdout writer:
with twenty pipelined tool calls, roughly half were never answered, and
the process exited 0 (modelcontextprotocol#2678).
EOF on stdin is a half-close, and the transport now says so: the stdio
read stream is marked `eof_is_half_close`, the server loop turns that into
the dispatcher's new `drain_on_eof`, and on a clean EOF the dispatcher
lets in-flight handlers run to completion and write their answers, keeps
their notifications flowing, and fails any new request to the peer at
once. There is no drain budget: a half-closed peer is owed every answer,
and how long to wait for the server is the client's decision, which the
spec's shutdown sequence already makes (close stdin, wait, kill). A full
close still cancels immediately, as do the cancellation and crash paths,
and every other transport keeps today's behaviour, since for them EOF
means the peer is gone.
A client that closes its end of the server's stdout has stopped reading,
whatever stdin says. Before, the broken pipe on the next write crashed out
of run_stdio_async as a nested exception group, and if the client also
held stdin open the exit never came, because the stdin reader sat in an
anyio pool thread that cancellation could not interrupt and that
interpreter exit would have joined. The stdout writer now treats a broken
pipe as the client leaving: it logs, cancels the transport's task group,
and the block exits normally, cutting short any in-flight requests, a
drain included. Stdin is read on a daemon thread of our own that hands
lines to the loop through a blocking portal, so a parked read can hold
neither run_stdio_async nor the process open; it ends once stdin does.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NVnAFCbYxmG2UVcP1g75Ko
Copy file name to clipboardExpand all lines: docs/run/index.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,8 @@ python server.py
39
39
40
40
Nothing prints, and it doesn't return. It is waiting on stdin for a host to speak first.
41
41
42
+
Closing that stdin is how a host stops the server. Requests it has already read are still answered: handlers in flight run to completion, their results go out on stdout, and only then does `run()` return. It also means `python server.py < requests.jsonl > responses.jsonl` answers every request in the file. How long to wait for that is the host's decision, not the server's: a host that has closed stdin and run out of patience kills the process, which is the shutdown sequence the spec describes. A host that closes the server's stdout instead has stopped reading, so the server stops at once, in-flight requests included.
43
+
42
44
That also means stdout **is the wire**. While serving, the SDK moves the wire to a private descriptor and diverts output that is *flushed* to stdout (a subprocess writing to its inherited stdout, a flushed `print()`) to stderr, where it can't corrupt the stream. Output flushed to stdout *before* serving begins (a wrapper script echoing, an unbuffered import-time print) still lands on the wire, and so does a `print()` that stays buffered until the interpreter drains it at exit. For output you actually want, the `logging` module is the right tool: its handler flushes each record to stderr as it happens. That story is in **[Logging](../handlers/logging.md)**.
0 commit comments