From 5f6dfe2d5c00c6dcdee3b0316c6d87ace565756a Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Sun, 6 Jul 2025 10:57:17 -0400 Subject: [PATCH] Replace asserts with runtime checks --- src/fastmcp/client/client.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.py index 4d773e36e..e78f2c404 100644 --- a/src/fastmcp/client/client.py +++ b/src/fastmcp/client/client.py @@ -295,7 +295,10 @@ class Client(Generic[ClientTransportT]): async with self._context_lock: need_to_start = self._session_task is None or self._session_task.done() if need_to_start: - assert self._nesting_counter == 0 + if self._nesting_counter != 0: + raise RuntimeError( + f"Internal error: nesting counter should be 0 when starting new session, got {self._nesting_counter}" + ) self._stop_event = anyio.Event() self._ready_event = anyio.Event() self._session_task = asyncio.create_task(self._session_runner()) @@ -303,7 +306,10 @@ class Client(Generic[ClientTransportT]): if self._session_task.done(): exception = self._session_task.exception() - assert exception is not None + if exception is None: + raise RuntimeError( + "Session task completed without exception but connection failed" + ) if isinstance(exception, httpx.HTTPStatusError): raise exception raise RuntimeError(