fix: close MCP stdio transports from agent task

This commit is contained in:
Xubin Ren
2026-06-22 18:59:50 +08:00
parent 6efef2700a
commit fbaa85117b
4 changed files with 227 additions and 78 deletions
+16 -5
View File
@@ -1105,16 +1105,23 @@ def _run_gateway(
console.print(f"[yellow]Could not open browser ({e}); visit {open_browser_url}[/yellow]")
async def run():
tasks: list[asyncio.Task] = []
try:
await cron.start()
tasks = [
agent.run(),
channels.start_all(),
asyncio.create_task(agent.run(), name="nanobot-agent-loop"),
asyncio.create_task(channels.start_all(), name="nanobot-channels"),
]
if health_server_enabled:
tasks.append(_health_server(config.gateway.host, port))
tasks.append(asyncio.create_task(
_health_server(config.gateway.host, port),
name="nanobot-health-server",
))
if open_browser_url:
tasks.append(_open_browser_when_ready())
tasks.append(asyncio.create_task(
_open_browser_when_ready(),
name="nanobot-open-browser",
))
await asyncio.gather(*tasks)
except KeyboardInterrupt:
console.print("\nShutting down...")
@@ -1124,9 +1131,13 @@ def _run_gateway(
console.print("\n[red]Error: Gateway crashed unexpectedly[/red]")
console.print(traceback.format_exc())
finally:
await agent.close_mcp()
cron.stop()
agent.stop()
for task in tasks:
if not task.done():
task.cancel()
if tasks:
await asyncio.gather(*tasks, return_exceptions=True)
await channels.stop_all()
# Flush all cached sessions to durable storage before exit.
# This prevents data loss on filesystems with write-back