Title: stdio server never exits on stdin EOF while the client leaves stdout unread · Issue #3674 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: stdio server never exits on stdin EOF while the client leaves stdout unread · Issue #3674 · modelcontextprotocol/python-sdk
X Title: stdio server never exits on stdin EOF while the client leaves stdout unread · Issue #3674 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before openi...
Open Graph Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex...
X Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/mod...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3674
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"stdio server never exits on stdin EOF while the client leaves stdout unread","articleBody":"### Initial Checks\n- [X] I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)\n- [X] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Release line\n\n2.x (current stable)\n\n### Description\n\nWhen a stdio client closes the server's stdin while a response is still being written to a full, unread stdout pipe, `MCPServer.run()` never returns. The process sits idle until it is signalled.\n\nSteps: the client sends a request whose response is larger than the pipe buffer, does not read stdout, then closes stdin. The server keeps running indefinitely. If the client drains stdout, the same sequence exits 0 right away.\n\nThread dump of the hung server (faulthandler): the main thread is idle in the event loop's `select`, and one AnyIO worker thread is inside `context.run(func, *args)`, blocked in the C-level `write(2)` behind `stdout_writer`'s `await stdout.write(...)` / `await stdout.flush()` in `mcp/server/stdio.py`. `AsyncFile.write` calls `to_thread.run_sync` without `abandon_on_cancel`. So when stdin EOF ends the session and the task group cancels `stdout_writer`, the cancellation waits for a write that can only finish if the client reads. `stdio.py` already expects this case (`closefd=False`, because \"a worker thread can still block on this descriptor after the transport exits\"). The exit path still waits on that thread, though.\n\nImpact is limited for clients that follow the spec's stdio shutdown, which escalates to SIGTERM and then SIGKILL. The TypeScript SDK client does this after 2 s per step. Any client that stops at \"close stdin and wait\" will leak the server process.\n\nThis is related to #2678 but is a different bug. #2678 is about responses lost on EOF, while this one is about the process never exiting on EOF. A fix for #2678 that drains pending responses before exiting needs a bound on that drain. Otherwise it would turn every undeliverable response into this hang.\n\n### Example Code\n\n```python\n# server.py\nfrom mcp.server.mcpserver import MCPServer\n\nmcp = MCPServer(\"eof-hang-repro\")\n\n\n@mcp.tool()\ndef big() -> str:\n \"\"\"Return a response larger than a pipe buffer.\"\"\"\n return \"x\" * 1_000_000\n\n\nif __name__ == \"__main__\":\n mcp.run()\n```\n\n```python\n# client.py: python client.py [--drain]\nimport json, subprocess, sys, threading, time\nfrom pathlib import Path\n\ndrain = \"--drain\" in sys.argv\nproc = subprocess.Popen([sys.executable, str(Path(__file__).with_name(\"server.py\"))],\n stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)\n\n\ndef send(message):\n proc.stdin.write((json.dumps(message) + \"\\n\").encode())\n proc.stdin.flush()\n\n\nsend({\"jsonrpc\": \"2.0\", \"id\": 0, \"method\": \"initialize\", \"params\": {\n \"protocolVersion\": \"2025-06-18\", \"capabilities\": {},\n \"clientInfo\": {\"name\": \"repro\", \"version\": \"0\"}}})\nproc.stdout.readline()\nsend({\"jsonrpc\": \"2.0\", \"method\": \"notifications/initialized\"})\nsend({\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"tools/call\", \"params\": {\"name\": \"big\", \"arguments\": {}}})\nif drain:\n threading.Thread(target=proc.stdout.read, daemon=True).start()\ntime.sleep(1)\nproc.stdin.close()\ntry:\n proc.wait(timeout=10)\n print(f\"drain={drain}: exited {proc.returncode} after stdin EOF\")\nexcept subprocess.TimeoutExpired:\n print(f\"drain={drain}: still running 10 s after stdin EOF\")\n proc.terminate()\n proc.wait()\n```\n\nOutput:\n\n```\n$ python client.py\ndrain=False: still running 10 s after stdin EOF\n$ python client.py --drain\ndrain=True: exited 0 after stdin EOF\n```\n\n`stdio.py` is the same in 2.2.0, 2.3.0 and current `main`. In the downstream project where this came up (kwin-mcp), the hang still occurs after the server's own signal handling was hardened, because it depends only on `run()` returning after EOF.\n\n### Python & MCP Python SDK\n\n```\nPython 3.14.7 (Fedora 44, Linux 7.2)\nmcp 2.3.0 (also reproduced on 2.2.0), anyio 4.15.1\n```\n\n---\n\u003csub>\u003cem>**[@superbiche](https://github.com/superbiche)** · user · drafted with Claude Opus 5.5, reviewed before posting; the voice and decisions are mine.\u003c/em>\u003c/sub>\n","author":{"url":"https://github.com/superbiche","@type":"Person","name":"superbiche"},"datePublished":"2026-10-10T20:15:58Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3674"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a513e57c-10d7-f460-bbed-3fa0e258af03 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8E0:63337:4AE5BA:69EEA1:6B0471B2 |
| html-safe-nonce | 072d20f71eb43db1eb081b5f51a7bddc176d822bf9933068f5a94e51494e3c0a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEUwOjYzMzM3OjRBRTVCQTo2OUVFQTE6NkIwNDcxQjIiLCJ2aXNpdG9yX2lkIjoiNTY2NDY3NjA5MTI1NjkyNjY0MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1124571ad9dd7c1f1f6029e591e820b64301d074e268f7e006bf2dce517f6e57 |
| hovercard-subject-tag | issue:5794912900 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3674/issue_layout |
| twitter:image | https://opengraph.githubassets.com/44b19f5739ae5dcbd58be258c28749c24726c94140e2277693d17d3517d472f9/modelcontextprotocol/python-sdk/issues/3674 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/44b19f5739ae5dcbd58be258c28749c24726c94140e2277693d17d3517d472f9/modelcontextprotocol/python-sdk/issues/3674 |
| og:image:alt | Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | superbiche |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width