Server hangs when shutting down if a connection is still open
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Tái hiện hành vi tắt với ví dụ FastMCP streamable-http được cung cấp và kiểm tra cách xử lý phiên trong streamable_http_manager.py được thể hiện trong các log. Theo dõi cách các kết nối đang hoạt động và các tác vụ được xử lý trong quá trình tắt, sau đó xác minh rằng CTRL+C thoát sạch sẽ trong khi Inspector vẫn được kết nối.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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 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.
We 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.
Shouldn't the server close its active connections/tasks on shutdown?
Reproduction
- Run the server (see code snippet)
- In the MCP Inspector click "connect"
- In the server's terminal hit CTRL+C
At this point
- 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 - Clicking "Disconned" in the Inspector cannot disconnect, because
Error in /mcp route: TypeError: fetch failed
Evidences
Shell execution
$ python demo-mcp.py
INFO: Started server process [10749]
INFO: Waiting for application startup.
[08/14/25 11:03:16] INFO StreamableHTTP session manager started streamable_http_manager.py:110
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
[08/14/25 11:03:21] INFO Created new transport with session ID: 4134f60ccda44baf8a01733ed94bc0d8 streamable_http_manager.py:233
INFO: 127.0.0.1:54629 - "POST /mcp HTTP/1.1" 200 OK
INFO: 127.0.0.1:54629 - "POST /mcp HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:54630 - "GET /mcp HTTP/1.1" 200 OK
^CINFO: Shutting down
INFO: Waiting for connections to close. (CTRL+C to force quit)
Inspector proxy Disconnect error
Received DELETE message for sessionId dd8a78d2-d5a8-4ff2-b679-6a8571f7cd0c
Connection refused. Is the MCP server running?
Error in /mcp route: TypeError: fetch failed
at node:internal/deps/undici/undici:13502:13
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async StreamableHTTPClientTransport.terminateSession (file://.../.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/client/streamableHttp.js:355:30)
at async file://.../.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:264:17 {
[cause]: Error: connect ECONNREFUSED 127.0.0.1:8000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1636:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8000
}
}
Mitigation
Setting 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 and currently available in FastMCP 2.0)
Example Code
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
async def hello() -> str:
return "world"
mcp.run(transport="streamable-http")
Python & MCP Python SDK
Python 3.12.8
mcp 1.12.4
Edit: reproduced with mcp 1.13.0
- Ngôn ngữ chính
- Python
- Star
- 24.3k
- Fork
- 4k
- Merge trung bình
- 1 ngày 19 phút
- Pull request đã merge (30 ngày)
- 29
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/python-sdk
-
Streamable HTTP client logs a WARNING for valid 202 Accepted on session termination (DELETE) Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
modelcontextprotocol/python-sdk#3546 · 5 bình luận ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
modelcontextprotocol/python-sdk#3545 · 1 bình luận ·
-
v1 v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
modelcontextprotocol/python-sdk#3508 · 2 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
modelcontextprotocol/python-sdk#3504 ·
-
v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
modelcontextprotocol/python-sdk#3492 · 1 bình luận ·
Tất cả issue của modelcontextprotocol/python-sdk
Issue tương tự
-
货币战争手改优先级配置缺少列表元素类型校验(P3) Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Đang mởarea: ci bug perceived difficulty: 3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
ClickHouse/clickhouse-connect#1057 ·