fix: guard against CancelledError on MCP startup future (Python 3.14) (#146)

On Python 3.14, Future.exception() raises CancelledError on cancelled
futures instead of returning None. Check future.cancelled() before
calling exception() to prevent crash when the MCP event loop shuts down
before _connect_all completes.
This commit is contained in:
Patrick Buckley
2026-03-21 01:06:11 -07:00
committed by GitHub
parent 04c50568e9
commit 756c4d8929
+5 -3
View File
@@ -162,9 +162,11 @@ class MCPClientManager:
future = asyncio.run_coroutine_threadsafe(self._connect_all(), self._loop)
self._connected.wait(timeout=30)
# Surface any exception from _connect_all (unlikely — per-server errors are caught)
if future.done() and future.exception():
self._error = str(future.exception())
log.error("MCP initialization error: %s", self._error)
if future.done() and not future.cancelled():
exc = future.exception()
if exc:
self._error = str(exc)
log.error("MCP initialization error: %s", self._error)
async def _connect_all(self) -> None:
"""Connect to every configured server (runs on the background loop)."""