Repository navigation
ClosedResourceError During MCP Stateless Server Shutdown When Sending Log Messages #1967
Copy link
Copy link
Closed
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
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
I'm seeing occasional errors from our MCP server, which I think are a race condition during shutdown.
I think this is a similar but distinct issue to the race condition in #1190 and #1384 as this is during send_log_message
I haven't been able to get a consistent reproduce yet, will update when I can.
Stack Trace
+ Exception Group Traceback (most recent call last):
| File "/app/.venv/lib/python3.13/site-packages/mcp/server/streamable_http_manager.py", line 180, in run_stateless_server
| await self.app.run(
| ...<4 lines>...
| )
| File "/app/.venv/lib/python3.13/site-packages/fastmcp/server/low_level.py", line 202, in run
| async with AsyncExitStack() as stack:
| ~~~~~~~~~~~~~~^^
| File "/usr/local/lib/python3.13/contextlib.py", line 768, in __aexit__
| raise exc
| File "/usr/local/lib/python3.13/contextlib.py", line 751, in __aexit__
| cb_suppress = await cb(*exc_details)
| ^^^^^^^^^^^^^^^^^^^^^^
| File "/app/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 238, in __aexit__
| return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "/app/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 783, in __aexit__
| raise BaseExceptionGroup(
| "unhandled errors in a TaskGroup", self._exceptions
| ) from None
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/app/.venv/lib/python3.13/site-packages/fastmcp/server/low_level.py", line 214, in run
| async with anyio.create_task_group() as tg:
| ~~~~~~~~~~~~~~~~~~~~~~~^^
| File "/app/.venv/lib/python3.13/site-packages/anyio/_backends/_asyncio.py", line 783, in __aexit__
| raise BaseExceptionGroup(
| "unhandled errors in a TaskGroup", self._exceptions
| ) from None
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/app/.venv/lib/python3.13/site-packages/mcp/server/lowlevel/server.py", line 694, in _handle_message
| await session.send_log_message(
| ...<3 lines>...
| )
| File "/app/.venv/lib/python3.13/site-packages/mcp/server/session.py", line 213, in send_log_message
| await self.send_notification(
| ...<10 lines>...
| )
| File "/app/.venv/lib/python3.13/site-packages/mcp/shared/session.py", line 335, in send_notification
| await self._write_stream.send(session_message)
| File "/app/.venv/lib/python3.13/site-packages/anyio/streams/memory.py", line 249, in send
| self.send_nowait(item)
| ~~~~~~~~~~~~~~~~^^^^^^
| File "/app/.venv/lib/python3.13/site-packages/anyio/streams/memory.py", line 218, in send_nowait
| raise ClosedResourceError
| anyio.ClosedResourceError
+------------------------------------Python & MCP Python SDK
MCP SDK Version: 1.25.0
via fastmcp Version: 2.14.4
Python Version: 3.13.5
Reactions are currently unavailable
Metadata
Metadata
Assignees
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
Activity
maxisbey commentedon Jan 28, 2026
Could you please provide a minimal reproducible example code snippet we can run to get the error?
felixweinberger commentedon Jan 28, 2026
thanks for the detailed stack trace. this does look like a distinct race from #1384 — the write stream is getting closed before the log notification can be sent.
can you share a minimal reproduction when you get one? ideally a small script we can run to trigger the shutdown race. info about your transport setup (streamable HTTP?) and what's triggering the log messages during shutdown would help too.
tokoyoda commentedon Feb 4, 2026
I hit what appears to be the same
ClosedResourceErroron mcp 1.26.0 with streamable-http transport (stateful mode). The crash occurs reliably whenever the MCP client disconnects while a long-running tool handler is still in progress. Below is a minimal reproduction and root cause analysis.Reproduction
Root Cause
terminate()closes_write_streamimmediately without waiting for in-flight handlers. When a handler finishes and callsrespond()→_send_response()→_write_stream.send(), it raisesClosedResourceErrorwhich propagates unhandled through theTaskGroup, crashing the session._handle_delete_request→terminate()closes all streams_receive_loop()exits itsasync withblock →_write_streamclosedServer.run()TaskGroupwaits for in-flight handlersmessage.respond()→_send_response()→_write_stream.send()→ClosedResourceErrorThe existing
ClosedResourceErrorcatch in_receive_looponly guards the read side, not the write path used by handlers.Note:
streamablehttp_clientdefaults toterminate_on_close=True, so every normal client disconnect sends DELETE — meaning any long-running tool handler will crash the session.fix: don't send log notification on transport error