From d83ba36800b844a13698f00d462cf8290f20fe60 Mon Sep 17 00:00:00 2001 From: cdkey85 Date: Thu, 19 Mar 2026 11:35:49 +0800 Subject: [PATCH] fix(agent): handle asyncio.CancelledError in message loop - Catch asyncio.CancelledError separately from generic exceptions - Re-raise CancelledError only when loop is shutting down (_running is False) - Continue processing messages if CancelledError occurs during normal operation - Prevents anyio/MCP cancel scopes from prematurely terminating the agent loop --- nanobot/agent/loop.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 36ab769..ea801b1 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -264,6 +264,12 @@ class AgentLoop: msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0) except asyncio.TimeoutError: continue + except asyncio.CancelledError: + # anyio/MCP cancel scopes surface as CancelledError (a BaseException subclass). + # Re-raise only if the loop itself is being shut down; otherwise keep running. + if not self._running: + raise + continue except Exception as e: logger.warning("Error consuming inbound message: {}, continuing...", e) continue