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
This commit is contained in:
@@ -264,6 +264,12 @@ class AgentLoop:
|
|||||||
msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0)
|
msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
continue
|
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:
|
except Exception as e:
|
||||||
logger.warning("Error consuming inbound message: {}, continuing...", e)
|
logger.warning("Error consuming inbound message: {}, continuing...", e)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user