René's URL Explorer Experiment


Title: ClosedResourceError + 'Unexpected ASGI message ... after response already completed' race in _handle_post_request notification path (streamable-http) · Issue #3641 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: ClosedResourceError + 'Unexpected ASGI message ... after response already completed' race in _handle_post_request notification path (streamable-http) · Issue #3641 · modelcontextprotocol/python-sdk

X Title: ClosedResourceError + 'Unexpected ASGI message ... after response already completed' race in _handle_post_request notification path (streamable-http) · Issue #3641 · modelcontextprotocol/python-sdk

Description: Bug: RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed following ClosedResourceError in _handle_post_request notification/response path Environment mcp: 2.1.1 starlette: 1.6.0 uvicorn: 0.5...

Open Graph Description: Bug: RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed following ClosedResourceError in _handle_post_request notification/response path Environment ...

X Description: Bug: RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed following ClosedResourceError in _handle_post_request notification/response path Envi...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3641

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ClosedResourceError + 'Unexpected ASGI message ... after response already completed' race in _handle_post_request notification path (streamable-http)","articleBody":"# Bug: `RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed` following `ClosedResourceError` in `_handle_post_request` notification/response path\n\n## Environment\n\n- `mcp`: 2.1.1\n- `starlette`: 1.6.0\n- `uvicorn`: 0.52.4\n- `anyio`: 4.14.2\n- Python: 3.13.14\n- OS: macOS 27.0.0 (Darwin, arm64)\n- Server: `basic-memory` 0.23.2 (FastMCP-based), `streamable-http` transport, stateful (not `stateless_http`), single uvicorn worker, run as a long-lived local service (launchd) on `127.0.0.1`.\n\n## Description\n\nOur server logs this pair of exceptions every 1-2 hours of normal multi-client usage (several unrelated local MCP clients making tool calls concurrently against the same long-running session). Each occurrence is almost always transient — the server keeps serving the next request fine within milliseconds — but on one occasion it coincided with the server task group getting wedged: the process stayed alive and listening, but stopped responding to any further request, accumulating CPU in a runnable state until we force-restarted it. I can't yet prove the wedge and this exception pair are causally linked (vs. coincidental timing), but I haven't found another candidate in the logs for that incident, so I'm reporting the reproducible half of it (the exception pair) and flagging the wedge as a possible consequence.\n\n### The two paired exceptions (always appear together, in this order)\n\n**1.** In `_handle_post_request`'s notification/response branch (around `streamable_http.py:588`, `await writer.send(session_message)`):\n\n```\nError handling POST request\nTraceback (most recent call last):\n  File \".../mcp/server/streamable_http.py\", line 588, in _handle_post_request\n    await writer.send(session_message)\n    ...\n    raise ClosedResourceError\nanyio.ClosedResourceError\n```\n\n**2.** Immediately after (same request, different connection, usually 1-4 seconds later):\n\n```\nERROR:    Exception in ASGI application\nTraceback (most recent call last):\n  File \".../mcp/server/streamable_http.py\", line 588, in _handle_post_request\n    await writer.send(session_message)\n    ...\n    raise RuntimeError(f\"Unexpected ASGI message '{message['type']}' sent, after response already completed.\")\nRuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed.\n```\n\n### Where this sits relative to known issues\n\nThis looks related to, but distinct from, the previously-reported/fixed races in this area:\n\n- #1363 / #1384 — message-router `ClosedResourceError` from early request validation failure. Already fixed and present in our installed `mcp` 2.1.1 (we checked: `except anyio.ClosedResourceError` guards are present in the message router).\n- #2064 / #2072 — `ClosedResourceError` from `send_log_message()` in the *error recovery* path after a client disconnect. Different code path (ours is the success/notification path, not error logging).\n- #2328 / #2334 — `respond()`-after-cancel assertion race, backported for v1.x. Different symptom (`AssertionError`, not a double ASGI response).\n\nOurs happens on the **notification/response branch** at line 588 — the code path that:\n1. Immediately sends a `202 Accepted` HTTP response for a non-`JSONRPCRequest` message (notification or response), then\n2. Forwards the message to `writer` for the session's message router to process.\n\nThe second exception (`Unexpected ASGI message 'http.response.start' sent, after response already completed`) implies something tried to start an ASGI response a second time on a connection that had already fully completed its response — i.e., there appear to be two handlers (or one handler re-entered) racing on the same underlying ASGI `send` callable, not just a stream that was already closed.\n\n## Hypothesis\n\nWith several concurrent clients attached to the same session, two POST requests can race such that:\n- Request A completes its `202 Accepted` response and returns.\n- Something tied to request A's scope/send (session teardown, or a delayed completion callback from the message router on the same `writer`) fires again afterward and tries to write `http.response.start` through the same (now-closed, already-completed) ASGI `send`, producing the `RuntimeError`.\n- The reactor appears to mix up these fast-fail paths tightly enough that it can spin instead of cleanly terminating, which is consistent with what we saw during the wedge (sustained CPU, no response to new requests, had to be killed externally).\n\nI don't have a minimal repro yet — this only shows up under organic concurrent load against a long-running stateful session, not in a quick scripted test. Happy to help instrument/reproduce if a maintainer can point at the likely callback path (e.g. is there a completion/cancel callback registered per-request that isn't being cancelled/unregistered when the `202` response path returns early?).\n\n## Impact\n\n- Usually harmless (logged, swallowed, next request succeeds).\n- At least once, coincided with the whole server task group wedging (high CPU, unresponsive, required external restart). Severity assessment is tentative since causation isn't proven, but flagging given the potential for a hang in a long-running server.\n\n## What we've done in the meantime\n\nRunning a watchdog that health-checks the MCP endpoint and does a bounded, backoff-limited restart (not an unconditional restart loop) with alerting if it doesn't recover after a few attempts.\n","author":{"url":"https://github.com/djbclark","@type":"Person","name":"djbclark"},"datePublished":"2026-10-03T18:56:43Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3641"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:509750ee-bb38-acae-30aa-08679144a00d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8F44:BEE52:BD3B5:F9E59:6B00B9A9
html-safe-nonce02b1205755d82eed2ab76ffe6e46a6e450626eba306f32fb1d36f8b8b95f2d37
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjQ0OkJFRTUyOkJEM0I1OkY5RTU5OjZCMDBCOUE5IiwidmlzaXRvcl9pZCI6Ijc1NDM0Njg4MzU3ODE3ODYwMjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac7fea9535aa1c6cc8463664e9c11ef9fea8df2b912748d315a424f77505726561
hovercard-subject-tagissue:5692599098
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3641/issue_layout
twitter:imagehttps://opengraph.githubassets.com/20687777978dd150ad76b584fdc22b00a6491c4108c1fb807ffb751b8a82cafd/modelcontextprotocol/python-sdk/issues/3641
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/20687777978dd150ad76b584fdc22b00a6491c4108c1fb807ffb751b8a82cafd/modelcontextprotocol/python-sdk/issues/3641
og:image:altBug: RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed following ClosedResourceError in _handle_post_request notification/response path Environment ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedjbclark
hostnamegithub.com
expected-hostnamegithub.com
None1d07c25977adfc5ff6acb0a962a9bc168c07afbda6fef765f9761c350c2ae4a2
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release5429cbfc6aca8c7b9a086a3c8f47400346f38cc9
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/3641#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3641
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3641
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3641
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3641
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/3641
modelcontextprotocolhttps://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Codehttps://github.com/modelcontextprotocol/python-sdk
Issues308 (308)https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests112 (112)https://github.com/modelcontextprotocol/python-sdk/pulls
Actionshttps://github.com/modelcontextprotocol/python-sdk/actions
Projectshttps://github.com/modelcontextprotocol/python-sdk/projects
Security and quality12 (12)https://github.com/modelcontextprotocol/python-sdk/security
Insightshttps://github.com/modelcontextprotocol/python-sdk/insights
https://github.com/modelcontextprotocol/python-sdk
New issuehttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3641
New issuehttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3641
ClosedResourceError + 'Unexpected ASGI message ... after response already completed' race in _handle_post_request notification path (streamable-http)https://github.com/modelcontextprotocol/python-sdk/issues/3641#top
v1Affects the v1.x maintenance linehttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v1%22
v2Affects the v2 line (2.x on main)https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
https://github.com/djbclark
djbclarkhttps://github.com/djbclark
on Oct 3, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#issue-5692599098
Race Condition in StreamableHTTP Transport Causes ClosedResourceError #1363https://github.com/modelcontextprotocol/python-sdk/issues/1363
fix: handle ClosedResourceError in StreamableHTTP message router #1384https://github.com/modelcontextprotocol/python-sdk/pull/1384
ClientDisconnect during _handle_post_request crashes stateless session with ClosedResourceError #2064https://github.com/modelcontextprotocol/python-sdk/issues/2064
fix: handle ClosedResourceError in _handle_message error recovery path #2072https://github.com/modelcontextprotocol/python-sdk/pull/2072
[Bug] Server crashes with anyio.ClosedResourceError when receiving raw invalid UTF-8 bytes #2328https://github.com/modelcontextprotocol/python-sdk/issues/2328
[v1.x] fix: handle ClosedResourceError when transport closes mid-request #2334https://github.com/modelcontextprotocol/python-sdk/pull/2334
claudehttps://github.com/apps/claude
v2Affects the v2 line (2.x on main)https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
v1Affects the v1.x maintenance linehttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v1%22
on Oct 3, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#event-32411670400
https://github.com/Ben-Home
Ben-Homehttps://github.com/Ben-Home
on Oct 5, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#issuecomment-5985852945
https://github.com/0xamlab
0xamlabhttps://github.com/0xamlab
on Oct 6, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#issuecomment-6018830942
https://github.com/maxisbey
maxisbeyhttps://github.com/maxisbey
on Oct 6, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#issuecomment-6020940900
Claudehttps://anthropic.com/claude-code
#3631https://github.com/modelcontextprotocol/python-sdk/issues/3631
#3631https://github.com/modelcontextprotocol/python-sdk/issues/3631
AI Disclaimerhttps://gist.github.com/maxisbey/6123d132484e4c533eab519a2800693d
Claude Codehttps://claude.ai/code
maxisbeyhttps://github.com/maxisbey
duplicatehttps://github.com/modelcontextprotocol/python-sdk/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Aduplicate
on Oct 6, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#event-32619041742
https://github.com/Varshith-Kali
Varshith-Kalihttps://github.com/Varshith-Kali
on Oct 6, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3641#issuecomment-6020976211
#3641https://github.com/modelcontextprotocol/python-sdk/issues/3641
Sign up for freehttps://github.com/signup?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3641
Sign in to commenthttps://github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3641
v1Affects the v1.x maintenance linehttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v1%22
v2Affects the v2 line (2.x on main)https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.