Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

SSE: ConnectionClosed exception after session disconnect #1227

Copy link
Copy link
@tomconte

Description

@tomconte
Issue body actions

Initial Checks

Description

In our environment, the SSE connections sometimes get dropped:

DEBUG:sse_starlette.sse:Got event: http.disconnect. Stop streaming.
DEBUG:root:Client session disconnected 081769ba-c683-471b-af22-1333409ab669

When this happens, on the next request for that session, we get a ClosedResourceError exception:

INFO: 100.100.0.154:49316 - "POST /messages/?session_id=081769bac683471baf221333409ab669 HTTP/1.1" 202 Accepted
ERROR:__main__:SSE post message error:
Traceback (most recent call last):
File "/app/mcp_renewables/server.py", line 714, in handle_post_message_with_error_handling
return await sse.handle_post_message(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.12/site-packages/mcp/server/sse.py", line 249, in handle_post_message
await writer. Send(session_message)
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 242, in send
self.send_nowait(item)
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 211, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError

I 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.

Example Code: the fix I applied

diff --git a/src/mcp/server/sse.py b/src/mcp/server/sse.py
index b7ff332..29acecb 100644
--- a/src/mcp/server/sse.py
+++ b/src/mcp/server/sse.py
@@ -179,7 +179,7 @@ class SseServerTransport:
 
         async with anyio.create_task_group() as tg:
 
-            async def response_wrapper(scope: Scope, receive: Receive, send: Send):
+            async def response_wrapper(scope: Scope, receive: Receive, send: Send, transport: SseServerTransport):
                 """
                 The EventSourceResponse returning signals a client close / disconnect.
                 In this case we close our side of the streams to signal the client that
@@ -190,10 +190,13 @@ class SseServerTransport:
                 )
                 await read_stream_writer.aclose()
                 await write_stream_reader.aclose()
+                await read_stream.aclose()
+                await write_stream.aclose()
+                transport._read_stream_writers.pop(session_id)
                 logging.debug(f"Client session disconnected {session_id}")
 
             logger.debug("Starting SSE response task")
-            tg.start_soon(response_wrapper, scope, receive, send)
+            tg.start_soon(response_wrapper, scope, receive, send, self)
 
             logger.debug("Yielding read and write streams")
             yield (read_stream, write_stream)

Python & MCP Python SDK

1.12.2

References

Related PRs:

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingSomething isn't workingneeds reproneeds additional information to be able to reproduce bugneeds additional information to be able to reproduce bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.