From 6faef9d96f505951942ac45b18618b2ebf1f70f6 Mon Sep 17 00:00:00 2001 From: Chris Guidry Date: Thu, 4 Dec 2025 12:05:55 -0500 Subject: [PATCH] Add timeout to client cleanup to prevent hangs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/fastmcp/client/client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.py index 90bbd32d9..c48b91e5d 100644 --- a/src/fastmcp/client/client.py +++ b/src/fastmcp/client/client.py @@ -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): """