Title: Race condition in StreamableHTTP: zero-buffer memory streams cause deadlock with concurrent SSE responses · Issue #1764 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Race condition in StreamableHTTP: zero-buffer memory streams cause deadlock with concurrent SSE responses · Issue #1764 · modelcontextprotocol/python-sdk
X Title: Race condition in StreamableHTTP: zero-buffer memory streams cause deadlock with concurrent SSE responses · Issue #1764 · 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 Bug Description SSE co...
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/1764
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Race condition in StreamableHTTP: zero-buffer memory streams cause deadlock with concurrent SSE responses","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### Bug Description\n\nSSE connections hang indefinitely when using StreamableHTTPServerTransport in stateless mode with responses containing 3+ items. The issue is caused by zero-buffer memory streams that block send() until receive() is called, creating a race condition between the response writer and the SSE stream iterator.\n\nRelated issues: #262 describes similar symptoms (client hangs on call_tool()) but root cause wasn't identified. This issue provides the specific cause and fix.\n\n### Expected Behavior\n\nAll tool responses should complete regardless of response size.\n\n### Actual Behavior\n- 1-2 items: Response returns immediately (~150ms)\n- 3+ items: Request hangs indefinitely (deadlock)\n\n### Root Cause Analysis\n\nThe issue is in mcp/server/streamable_http.py:\n\nLine 412 - zero-buffer request stream\nself._request_streams[request_id] = anyio.create_memory_object_stream[EventMessage](0)\n\nLine 460 - zero-buffer SSE stream\nsse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream[dict[str, str]](0)\n\n### Race condition flow:\n1. tg.start_soon(response, ...) starts SSE response task (non-blocking)\n2. await writer.send(session_message) sends request to MCP server\n3. MCP server processes quickly and calls message_router\n4. message_router tries await request_streams[id][0].send(EventMessage(...))\n5. DEADLOCK: If SSE writer hasn't started iterating yet, send() blocks forever\n\nWith zero-buffer streams, send() blocks until the receiver calls receive(). When the MCP server processes faster than the SSE writer task starts, deadlock occurs.\n\nWhy timing matters:\n- Small responses (1-2 items): SSE writer task starts before MCP response arrives → works\n- Larger responses (3+ items): MCP processes faster → response arrives before SSE iterator starts → blocked forever\n\n### Proposed Fix\nIncrease buffer size from 0 to a reasonable value (e.g., 10 or 100):\n\nLine 412\nself._request_streams[request_id] = anyio.create_memory_object_stream[EventMessage](10)\n\nLine 460\nsse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream[dict[str, str]](10)\n\nAlternative fix: Use await tg.start() instead of tg.start_soon() to ensure SSE writer is ready before sending requests (requires EventSourceResponse to support task status protocol).\n\n### Workaround\n\nWe've applied this fix via sed patch in our Dockerfile:\n\nRUN sed -i 's/create_memory_object_stream\\[EventMessage\\](0)/create_memory_object_stream[EventMessage](10)/g' \\\n /usr/local/lib/python3.11/site-packages/mcp/server/streamable_http.py && \\\n sed -i 's/create_memory_object_stream\\[dict\\[str, str\\]\\](0)/create_memory_object_stream[dict[str, str]](10)/g' \\\n /usr/local/lib/python3.11/site-packages/mcp/server/streamable_http.py\n This resolves the issue in our production environment.\n\n### Example Code\n\n```Python\nfrom fastmcp import FastMCP\nimport json\n\nmcp = FastMCP(\"test-server\")\n\n@mcp.tool()\nasync def test_tool() -> str:\n # Returns JSON with 3+ items - will hang\n return json.dumps({\n \"results\": [{\"n\": \"a\"}, {\"n\": \"b\"}, {\"n\": \"c\"}]\n })\n\napp = mcp.http_app(path=\"/mcp\", stateless_http=True)\n\n1. Call the tool via HTTP POST to /mcp\n2. Response hangs indefinitely for tools returning 3+ items in arrays\n3. Tools returning 1-2 items work correctly\n```\n\n### Python & MCP Python SDK\n\n```Text\n- MCP SDK version: 1.23.3\n- Python version: 3.11\n- FastMCP version: 2.13.1\n- Transport: StreamableHTTP with stateless_http=True\n```","author":{"url":"https://github.com/Ctariy","@type":"Person","name":"Ctariy"},"datePublished":"2025-12-10T13:47:46Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/1764"}
| 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:8e51b879-fad3-ec9b-a919-09bbaafbe99b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5A4:212F96:43DA90:59B37F:6AFCEAFA |
| html-safe-nonce | ca1ab4330f86f38d7ba80a76fd9487f264e730527ac686bb9f80b67def53b813 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUE0OjIxMkY5Njo0M0RBOTA6NTlCMzdGOjZBRkNFQUZBIiwidmlzaXRvcl9pZCI6Ijc5ODA4MDA0NDM2OTc2NzE2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | da99234f07016bcf5da3a450d73323c9cbae5020b725c2885b3e24c20555e912 |
| hovercard-subject-tag | issue:3715283854 |
| 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/1764/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7147c1f668019f2ab035908728ecb0618e203fe2cf1042c6a01f42d9bdca2743/modelcontextprotocol/python-sdk/issues/1764 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7147c1f668019f2ab035908728ecb0618e203fe2cf1042c6a01f42d9bdca2743/modelcontextprotocol/python-sdk/issues/1764 |
| 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 | Ctariy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 99aede4c5f55ec5cf06666a8c9e03ebf9f85f7231223e7f1c38c16e5504835b3 |
| 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 | ecfd63ca125ec74b42f74eeb158b3fa081c8c497 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width