Jeremiah Lowin commited on
Commit
91dbb6a
·
1 Parent(s): 3b238c3

Replace asserts with runtime checks

Browse files
Files changed (1) hide show
  1. src/fastmcp/client/client.py +8 -2
src/fastmcp/client/client.py CHANGED
@@ -295,7 +295,10 @@ class Client(Generic[ClientTransportT]):
295
  async with self._context_lock:
296
  need_to_start = self._session_task is None or self._session_task.done()
297
  if need_to_start:
298
- assert self._nesting_counter == 0
 
 
 
299
  self._stop_event = anyio.Event()
300
  self._ready_event = anyio.Event()
301
  self._session_task = asyncio.create_task(self._session_runner())
@@ -303,7 +306,10 @@ class Client(Generic[ClientTransportT]):
303
 
304
  if self._session_task.done():
305
  exception = self._session_task.exception()
306
- assert exception is not None
 
 
 
307
  if isinstance(exception, httpx.HTTPStatusError):
308
  raise exception
309
  raise RuntimeError(
 
295
  async with self._context_lock:
296
  need_to_start = self._session_task is None or self._session_task.done()
297
  if need_to_start:
298
+ if self._nesting_counter != 0:
299
+ raise RuntimeError(
300
+ f"Internal error: nesting counter should be 0 when starting new session, got {self._nesting_counter}"
301
+ )
302
  self._stop_event = anyio.Event()
303
  self._ready_event = anyio.Event()
304
  self._session_task = asyncio.create_task(self._session_runner())
 
306
 
307
  if self._session_task.done():
308
  exception = self._session_task.exception()
309
+ if exception is None:
310
+ raise RuntimeError(
311
+ "Session task completed without exception but connection failed"
312
+ )
313
  if isinstance(exception, httpx.HTTPStatusError):
314
  raise exception
315
  raise RuntimeError(