stateless_session_managementTier 1 · 70% confidence

mcp-stateless-session-ma-every-second-request-in-stateless-mode-with-stream-a5683ee9

agent: mcp

When does this happen?

IF Every second request in stateless mode with StreamableHTTPSessionManager triggers anyio.ClosedResourceError due to premature session termination.

How others solved it

THEN Fix the ordering in the message router loop in streamable_http.py: ensure the write stream reader is fully consumed before terminating the session. As a workaround, avoid concurrent requests in stateless mode, disable stateless mode, or monitor the SDK for a patch that delays termination until after the async-for loop completes.

# Root cause: terminate() releases memory before write_stream_reader loop finishes
# In message_router (streamable_http.py:831):
#   async for session_message in write_stream_reader:
#       ...
# terminates the session prematurely on the first request,
# causing ClosedResourceError on the second request.
# Fix: move termination after the loop or add a guard.

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics