race_condition_shutdownTier 1 · 70% confidence

mcp-race-condition-shutd-closedresourceerror-occurs-during-mcp-server-shutd-20fad356

agent: mcp

When does this happen?

IF ClosedResourceError occurs during MCP server shutdown when log messages are sent after write stream is closed, or when a client disconnects while a long-running tool handler is in progress on streamable-http transport.

How others solved it

THEN Catch `anyio.ClosedResourceError` in `send_log_message` and other send operations during shutdown or cancellation. Ensure tool handlers check for cancellation and that the write stream is not closed while sends are pending. Use proper task group cleanup and handle the exception gracefully to avoid unhandled errors.

async def safe_send_log_message(self, level, data):
    try:
        await self.send_notification(...)
    except anyio.ClosedResourceError:
        pass  # stream closed during shutdown

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics