Title: SSE: ConnectionClosed exception after session disconnect · Issue #1227 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: SSE: ConnectionClosed exception after session disconnect · Issue #1227 · modelcontextprotocol/python-sdk
X Title: SSE: ConnectionClosed exception after session disconnect · Issue #1227 · 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 In our environment, th...
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/1227
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SSE: ConnectionClosed exception after session disconnect","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\nIn our environment, the SSE connections sometimes get dropped:\n\n```\nDEBUG:sse_starlette.sse:Got event: http.disconnect. Stop streaming.\nDEBUG:root:Client session disconnected 081769ba-c683-471b-af22-1333409ab669\n```\n\nWhen this happens, on the next request for that session, we get a `ClosedResourceError` exception:\n\n```\nINFO: 100.100.0.154:49316 - \"POST /messages/?session_id=081769bac683471baf221333409ab669 HTTP/1.1\" 202 Accepted\nERROR:__main__:SSE post message error:\nTraceback (most recent call last):\nFile \"/app/mcp_renewables/server.py\", line 714, in handle_post_message_with_error_handling\nreturn await sse.handle_post_message(*args, **kwargs)\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nFile \"/app/.venv/lib/python3.12/site-packages/mcp/server/sse.py\", line 249, in handle_post_message\nawait writer. Send(session_message)\nFile \"/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py\", line 242, in send\nself.send_nowait(item)\nFile \"/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py\", line 211, in send_nowait\nraise ClosedResourceError\nanyio.ClosedResourceError\n```\n\nI believe the reason is that when the session disconnects, its file handles are not properly removed as explained in #703. I have forked the repo and applied the fix and it seems to solve the problem.\n\n\n\n### Example Code: the fix I applied\n\n```Python\ndiff --git a/src/mcp/server/sse.py b/src/mcp/server/sse.py\nindex b7ff332..29acecb 100644\n--- a/src/mcp/server/sse.py\n+++ b/src/mcp/server/sse.py\n@@ -179,7 +179,7 @@ class SseServerTransport:\n \n async with anyio.create_task_group() as tg:\n \n- async def response_wrapper(scope: Scope, receive: Receive, send: Send):\n+ async def response_wrapper(scope: Scope, receive: Receive, send: Send, transport: SseServerTransport):\n \"\"\"\n The EventSourceResponse returning signals a client close / disconnect.\n In this case we close our side of the streams to signal the client that\n@@ -190,10 +190,13 @@ class SseServerTransport:\n )\n await read_stream_writer.aclose()\n await write_stream_reader.aclose()\n+ await read_stream.aclose()\n+ await write_stream.aclose()\n+ transport._read_stream_writers.pop(session_id)\n logging.debug(f\"Client session disconnected {session_id}\")\n \n logger.debug(\"Starting SSE response task\")\n- tg.start_soon(response_wrapper, scope, receive, send)\n+ tg.start_soon(response_wrapper, scope, receive, send, self)\n \n logger.debug(\"Yielding read and write streams\")\n yield (read_stream, write_stream)\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\n1.12.2\n```\n\n### References\n\nRelated PRs:\n\n- #612 \n- #703 ","author":{"url":"https://github.com/tomconte","@type":"Person","name":"tomconte"},"datePublished":"2025-08-01T08:10:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1227/python-sdk/issues/1227"}
| 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:45e1dc08-7a4e-9459-e307-5dbd2f5994a7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ABA6:1D5981:41625D:572D60:6A5B89F4 |
| html-safe-nonce | b02719ad4e50e3853ee3ffa9b3d5d18e45af116c8ddccfa6781efc96ebeced14 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkE2OjFENTk4MTo0MTYyNUQ6NTcyRDYwOjZBNUI4OUY0IiwidmlzaXRvcl9pZCI6Ijc0MjQ2NTUxNjI1OTY4ODcwMjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 493c9efce8faa932933847ec6af9b6ab30bc4afb8e2dda1e56a522eb6b8a0b2a |
| hovercard-subject-tag | issue:3282997229 |
| 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/1227/issue_layout |
| twitter:image | https://opengraph.githubassets.com/43fb4a0e77ca54b4261c4fca34f376bed8ecb6ef9d937f0d23575fecd138c973/modelcontextprotocol/python-sdk/issues/1227 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/43fb4a0e77ca54b4261c4fca34f376bed8ecb6ef9d937f0d23575fecd138c973/modelcontextprotocol/python-sdk/issues/1227 |
| 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 | tomconte |
| 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