From a2acacd8f2a2395438954877062499bfb424e16a Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Sat, 14 Mar 2026 18:14:35 +0800 Subject: [PATCH] fix: add exception handling to prevent agent loop crash --- nanobot/agent/loop.py | 3 +++ nanobot/cli/commands.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index e05a73e..ed28a9e 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -258,6 +258,9 @@ class AgentLoop: msg = await asyncio.wait_for(self.bus.consume_inbound(), timeout=1.0) except asyncio.TimeoutError: continue + except Exception as e: + logger.warning("Error consuming inbound message: {}, continuing...", e) + continue cmd = msg.content.strip().lower() if cmd == "/stop": diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index d8aa411..685c1be 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -564,6 +564,10 @@ def gateway( ) except KeyboardInterrupt: console.print("\nShutting down...") + except Exception: + import traceback + console.print("\n[red]Error: Gateway crashed unexpectedly[/red]") + console.print(traceback.format_exc()) finally: await agent.close_mcp() heartbeat.stop()