Title: Server hangs when shutting down if a connection is still open · Issue #1272 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Server hangs when shutting down if a connection is still open · Issue #1272 · modelcontextprotocol/python-sdk
X Title: Server hangs when shutting down if a connection is still open · Issue #1272 · 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 Hello! I was testing m...
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/1272
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Server hangs when shutting down if a connection is still open","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\nHello!\n\nI was testing my MCP server with the inspector `npx @modelcontextprotocol/inspector` using streamable-http and I noticed that if the inspector is connected, the MCP Server will not shutdown.\n\nWe can't keep the server running if a client forgot to disconnect right? This is tested with the official inspector tools, so there might be other clients working the same way.\n\nShouldn't the server close its active connections/tasks on shutdown?\n\n### Reproduction\n\n1. Run the server (see code snippet)\n2. In the MCP Inspector click \"connect\"\n3. In the server's terminal hit CTRL+C\n\nAt this point\n- The server shows `Waiting for connections to close (CTRL+C to force quit)` and CTRL+C doesn't work, in fact nothing works except killing the process\n- Clicking \"Disconned\" in the Inspector cannot disconnect, because `Error in /mcp route: TypeError: fetch failed`\n\n### Evidences\n\nShell execution\n\n```shell\n$ python demo-mcp.py \nINFO: Started server process [10749]\nINFO: Waiting for application startup.\n[08/14/25 11:03:16] INFO StreamableHTTP session manager started streamable_http_manager.py:110\nINFO: Application startup complete.\nINFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n[08/14/25 11:03:21] INFO Created new transport with session ID: 4134f60ccda44baf8a01733ed94bc0d8 streamable_http_manager.py:233\nINFO: 127.0.0.1:54629 - \"POST /mcp HTTP/1.1\" 200 OK\nINFO: 127.0.0.1:54629 - \"POST /mcp HTTP/1.1\" 202 Accepted\nINFO: 127.0.0.1:54630 - \"GET /mcp HTTP/1.1\" 200 OK\n^CINFO: Shutting down\nINFO: Waiting for connections to close. (CTRL+C to force quit)\n```\n\nInspector proxy Disconnect error\n```\nReceived DELETE message for sessionId dd8a78d2-d5a8-4ff2-b679-6a8571f7cd0c\nConnection refused. Is the MCP server running?\nError in /mcp route: TypeError: fetch failed\n at node:internal/deps/undici/undici:13502:13\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async StreamableHTTPClientTransport.terminateSession (file://.../.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/streamableHttp.js:355:30)\n at async file://.../.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:264:17 {\n [cause]: Error: connect ECONNREFUSED 127.0.0.1:8000\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1636:16) {\n errno: -61,\n code: 'ECONNREFUSED',\n syscall: 'connect',\n address: '127.0.0.1',\n port: 8000\n }\n}\n```\n\n### Mitigation\n\nSetting the `timeout_graceful_shutdown` uvicorn config to a few seconds will eventually quit the process, **BUT** this is exiting with a non 0 exit code, which is an issue trying to run this in production. (This setting has been request [here](https://github.com/modelcontextprotocol/python-sdk/issues/253) and currently available in FastMCP 2.0)\n\n\n### Example Code\n\n```Python\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"demo\")\n\n@mcp.tool()\nasync def hello() -> str:\n return \"world\"\n\nmcp.run(transport=\"streamable-http\")\n```\n\n### Python & MCP Python SDK\n\n```Text\nPython 3.12.8\nmcp 1.12.4\n```\n\n**Edit**: reproduced with mcp 1.13.0","author":{"url":"https://github.com/lindycoder","@type":"Person","name":"lindycoder"},"datePublished":"2025-08-14T15:18:25Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/1272"}
| 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:f3bdab5f-cd77-b938-9959-c408a8b9cef8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BA34:2CFBF7:180D1EA:21ACFE3:6AFCF893 |
| html-safe-nonce | f7bd8ac580ca05132969c8b765f0b378911ecad7d2d72bdef1b2be375d5adfe4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQTM0OjJDRkJGNzoxODBEMUVBOjIxQUNGRTM6NkFGQ0Y4OTMiLCJ2aXNpdG9yX2lkIjoiOTE3NjE4MTE0NTYxNDQ4MTU1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 817cfc494e1ce7d42289cca31e40125eb77a064495e8512b4c36529879e24ad0 |
| hovercard-subject-tag | issue:3322609936 |
| 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/1272/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e4a3907f927b9b9c320b5c01b9268d633e87f4a6ecbf1aa9a7d6a170ea2a90f/modelcontextprotocol/python-sdk/issues/1272 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e4a3907f927b9b9c320b5c01b9268d633e87f4a6ecbf1aa9a7d6a170ea2a90f/modelcontextprotocol/python-sdk/issues/1272 |
| 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 | lindycoder |
| hostname | github.com |
| expected-hostname | github.com |
| None | cfabad6ee26d2b84c7afef4ae0cc71b8b692fb9d66f9f15997652ef991bfd3f4 |
| 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 | 35f85b78676038389558a41adf8d76e474592ede |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width