Title: Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown) · Issue #2231 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown) · Issue #2231 · modelcontextprotocol/python-sdk
X Title: Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown) · Issue #2231 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue Description Stdio transport: MCP s...
Open Graph Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...
X Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening t...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/2231
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown)","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\n## Stdio transport: MCP server process survives parent death (stdin EOF not causing shutdown)\n\n### Description\n\nWhen an MCP server using `transport=\"stdio\"` has its parent process die (e.g. the MCP client exits, is killed, or crashes), the server process is orphaned and continues running indefinitely. It gets reparented to init/systemd and never exits.\n\n### Expected behavior\n\nWhen the parent process dies and stdin reaches EOF, the server should detect this and shut down cleanly.\n\n### Analysis\n\nLooking at the code flow in `mcp/server/stdio.py`:\n\n1. `stdin_reader()` (line 60) iterates `async for line in stdin` where `stdin` is `anyio.wrap_file(TextIOWrapper(sys.stdin.buffer, encoding=\"utf-8\"))`\n2. When stdin reaches EOF, the loop should end, closing `read_stream_writer`\n3. This should close the `read_stream`, causing `server.run()`'s `async for message in session.incoming_messages` loop to end\n4. `run()` returns → `stdio_server()` context exits → task group cancels `stdout_writer` → process exits\n\nIn practice, the process survives. The likely cause is that `anyio.wrap_file` runs the underlying `readline()` in a worker thread, and this thread may not wake up or propagate EOF correctly when the pipe is closed due to parent death.\n\n### Reproduction\n\n```python\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"test\")\n\n@mcp.tool()\nasync def hello() -\u003e str:\n return \"hello\"\n\nmcp.run(transport=\"stdio\")\n```\n\n1. Start the server as a subprocess from any parent process\n2. Kill the parent process (e.g. `kill -9 \u003cparent_pid\u003e`)\n3. Observe that the MCP server process is still running (`ps aux | grep python`)\n4. The process is now reparented to init/systemd and will never exit\n\n### Environment\n\n- `mcp` version: 1.26.0\n- Python: 3.14\n- OS: Linux (Fedora 43)\n\n### Fix\n\nA proposed fix would be to monitor `stdin` via a selectors and when it becomes ready and returns \"\" shutdown the process.\n\n### Example Code\n\n```Python\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"test\")\n\n@mcp.tool()\nasync def hello() -\u003e str:\n return \"hello\"\n\nmcp.run(transport=\"stdio\")\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\nEnvironment\n\n mcp version: 1.26.0\n Python: 3.14\n OS: Linux (Fedora 43)\n```","author":{"url":"https://github.com/arcivanov","@type":"Person","name":"arcivanov"},"datePublished":"2026-03-06T11:53:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/2231/python-sdk/issues/2231"}
| 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:e626e92c-4e94-8ac6-ebb0-927959140456 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD72:190AD7:3DFBD5:51728D:6A5B383F |
| html-safe-nonce | ed6936ea9972b949eb69ea323204b4f1e4d2a4b93c20ea0bfc2d15df69084676 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDcyOjE5MEFENzozREZCRDU6NTE3MjhEOjZBNUIzODNGIiwidmlzaXRvcl9pZCI6IjIzOTI2OTAyNDMzMTUzODIzMzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8fee0a34a9b358eb3e706fb2ffdf644f07f8390d074b63ffccfc794653f5a979 |
| hovercard-subject-tag | issue:4033813323 |
| 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/2231/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f2945ce79fda1637e75622fdcf9d0657df2053832e705e12a4fb6f685248b8d4/modelcontextprotocol/python-sdk/issues/2231 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f2945ce79fda1637e75622fdcf9d0657df2053832e705e12a4fb6f685248b8d4/modelcontextprotocol/python-sdk/issues/2231 |
| og:image:alt | Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | arcivanov |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width