Skip to content

Your lightweight open-source personal agent.

fix: add exception handling to prevent agent loop crash

chengyongrua2acacd8f2a2
diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py
index e05a73e49..ed28a9e89 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 d8aa41156..685c1bebf 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()