Add timeout to client cleanup to prevent hangs

When the MCP SDK's transport tries to terminate a session during cleanup,
it can hang if the server is unresponsive (e.g., rate-limited). Adding a
5-second timeout ensures we don't block forever during `__aexit__`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chris Guidry 2025-12-04 12:05:55 -05:00
commit 6faef9d96f

View file

@ -374,7 +374,11 @@ class Client(Generic[ClientTransportT]):
return await self._connect()
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._disconnect()
# Use a timeout to prevent hanging during cleanup if the connection is in a bad
# state (e.g., rate-limited). The MCP SDK's transport may try to terminate the
# session which can hang if the server is unresponsive.
with anyio.move_on_after(5):
await self._disconnect()
async def _connect(self):
"""