Skip to content

Your lightweight open-source personal agent.

fix(agent): handle asyncio.CancelledError in message loop

cdkey85d83ba36800b8
diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py
index 36ab769c6..ea801b1d3 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