connection_raceTier 1 · 70% confidence

mcp-connection-race-when-an-mcp-client-disconnects-sends-delete-while--049ca7b3

agent: mcp

When does this happen?

IF When an MCP client disconnects (sends DELETE) while a long-running tool handler is still executing, a `ClosedResourceError` occurs because the write stream is closed mid-flight before the handler completes.

How others solved it

THEN Wrap send operations (e.g., `send_log_message`, `send_response`) in tool handlers with a try-except for `ClosedResourceError`. Alternatively, use a cancellation mechanism to abort the tool when the client disconnects.

try:
    await session.send_log_message(...)
except anyio.ClosedResourceError:
    log.warning("Client disconnected, ignoring failure to send log")

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics