fix(gateway): harden shared client lifecycle

This commit is contained in:
Xubin Ren committed 2026-08-17 20:56:10 +08:00
1 parent 19be5be1c0
commit cd6a11b3c5
11 files changed
+468 -34

No files matched your search

+19 -1
View File
@@ -389,7 +389,13 @@ def _run_gateway(
# Use the same runtime identity for foreground and managed gateway processes.
from nanobot.config.loader import get_config_path
from nanobot.gateway.runtime import GatewayRuntime, GatewayRuntimePaths, GatewayStartOptions
from nanobot.gateway.runtime import (
GatewayClientLease,
GatewayRuntime,
GatewayRuntimePaths,
GatewayStartOptions,
monitor_gateway_clients,
)
config_path = str(get_config_path().resolve(strict=False))
gateway_workspace = (
@@ -872,6 +878,14 @@ def _run_gateway(
finally:
await mcp_provider.aclose()
async def _monitor_local_clients() -> None:
orphaned = await monitor_gateway_clients(
GatewayClientLease(gateway_runtime, kind="gateway-monitor"),
shutdown_event,
)
if orphaned:
logger.info("Last local client disappeared; stopping on-demand gateway")
tasks = [
asyncio.create_task(
watch_config_file(
@@ -890,6 +904,10 @@ def _run_gateway(
),
name="nanobot-local-triggers",
),
asyncio.create_task(
_monitor_local_clients(),
name="nanobot-gateway-client-monitor",
),
]
if health_server_enabled:
tasks.append(asyncio.create_task(