Skip to content

Commit 6faef9d

Browse files
chrisguidryclaude
andcommitted
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 <[email protected]>
1 parent a8bbd74 commit 6faef9d

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/fastmcp/client/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ async def __aenter__(self):
374374
return await self._connect()
375375

376376
async def __aexit__(self, exc_type, exc_val, exc_tb):
377-
await self._disconnect()
377+
# Use a timeout to prevent hanging during cleanup if the connection is in a bad
378+
# state (e.g., rate-limited). The MCP SDK's transport may try to terminate the
379+
# session which can hang if the server is unresponsive.
380+
with anyio.move_on_after(5):
381+
await self._disconnect()
378382

379383
async def _connect(self):
380384
"""

0 commit comments

Comments
 (0)