Windows stdio server: subprocess spawned inside a tool handler hangs forever when it inherits the server stdin (stdin=DEVNULL fixes)
What happens
A subprocess spawned from INSIDE a FastMCP tool handler (stdio server, Windows) hangs forever when the child inherits the server stdin — which is the default in subprocess.run/create_subprocess_exec if stdin is not set.
Repro (Windows, minimal)
FastMCP server (stdio) with a tool that spawns a trivial child (e.g. python -c "print(1)"):
subprocess.run([...])with inherited stdin → child never completes; tool call hangs (abort only via MCP idle-timeout — 30 min by default in Claude Code)- same call with
stdin=subprocess.DEVNULL→ completes in ~0.6 s
A/B tested with FastMCP pure (zero user imports) to rule out user code; also reproduced via asyncio.create_subprocess_exec on the event loop. The moment the child stops sharing the server jsonrpc stdin, everything works.
Suggested guard
Document/force stdin=DEVNULL for subprocesses spawned in handlers, or detach the child stdin by default in any subprocess helper the SDK provides — inheriting the protocol pipe is never what a tool wants.
Environment: Windows 11 (26200), Python 3.14, mcp/fastmcp stdio transport, Claude Code 2.1.310 host.
The suggested guard is right, and the reason it hangs is worth stating explicitly, because it changes what the SDK should promise.
In a stdio server, process stdin is the JSON-RPC transport. A child spawned with inherited stdin shares that same handle, so there are now two readers on one pipe. What the child's runtime does with it varies (the Windows launcher, node, or a shell may read ahead to buffer), and any read it performs consumes bytes the server transport was waiting for. The tool call then waits for a response the transport can no longer assemble, and the client waits behind it. DEVNULL works because it removes the second reader, not because the child was waiting for input.
Two things worth adding to the issue, both cheap to confirm:
- This is not Windows-specific in principle, only in visibility. On POSIX the child inherits the same descriptor, and if it reads, frames go missing the same way. Windows is where it surfaces first because process creation defaults differ and the hang is total rather than partial.
- The inverse case has the same root cause and a different symptom: a child that holds the inherited stdin open without reading it keeps the transport from ever seeing EOF. That blocks clean shutdown on client disconnect, and it usually shows up as a leaked process rather than a hang.
On where the fix belongs, forcing DEVNULL inside the SDK is the safer default but it is a behaviour change, and anyone whose child legitimately reads the parent's stdin as data would be surprised. A middle path costs little:
- a documented subprocess helper in the SDK that sets stdin=DEVNULL by default, with the parameter named so opting out is explicit;
- a debug-level warning when a handler spawns a child without setting stdin at all, since that is the case that bites.
The second one is what turns a 30 minute hang into a log line, and it needs no behavioural change to ship.
Confirmed on Windows 11 (26200), Python 3.12.10, mcp 1.30.0: the inherit tool hangs, the stdin=DEVNULL one returns in ~0.4s.
One correction on the cause: it isn't a second reader on the pipe. Popen returns immediately, and hostname.exe / cmd /c echo 1 children complete fine. Only a Python child freezes, and it stays alive at ~4 MB, stuck in interpreter startup. That is CPython gh-78961 (synchronous pipe I/O is serialized on Windows, and the server always has a blocking read pending on stdin), so this looks like a duplicate of #671.
It's already fixed on main by #3117: the same repro against 2.3.1.dev17 returns in ~0.7s. v1.x doesn't have it (no stdin diversion in src/mcp/server/stdio.py). On v1 the workaround is stdin=subprocess.DEVNULL in the handler.
If a backport to v1.x is wanted, I'd be glad to do it. Otherwise this can be closed in favour of #671.
Disclosure: I investigated this with the help of an AI coding assistant (Claude Code). I ran every repro above myself on my machine.
Hi, we are TechAura. We would like to work on this if a maintainer is willing to assign it to us. Our read of the problem: in a stdio server, a subprocess spawned inside a tool handler inherits the server stdin and blocks waiting on it, while passing stdin=DEVNULL returns immediately. The fix we have in mind is to stop tool-handler-spawned subprocesses from inheriting server stdin, defaulting to DEVNULL, with a regression test covering the hang case. Happy to adjust the approach if the maintainers prefer a different shape.
Thanks for the interest! A note so effort isn't spent on the wrong fix: the hang isn't the child waiting on stdin. A Python child freezes during interpreter startup because synchronous pipe I/O is serialized on Windows (CPython gh-78961), which makes this a duplicate of #671.
That's already fixed on main by #3117 (my repro returns in ~0.7s on 2.3.1.dev17). So the only remaining work is a backport to v1.x, which doesn't have the stdin diversion in src/mcp/server/stdio.py.
As I offered on Oct 31, I'm happy to do that backport, with a regression test for the hang, once a maintainer assigns it. If maintainers would rather not backport, closing this in favour of #671 also works.
Disclosure: drafted with the help of an AI coding assistant (Claude Code); the repro results are from my own runs.
Thank you for the detailed analysis — you are right, and I am withdrawing my claim. The hang is the Windows pipe-serialization issue from #671, already fixed on main by #3117, so the DEVNULL change I proposed is the wrong fix. The v1.x backport you offered is the remaining work, and I will leave that to you once a maintainer assigns it.