Skip to content

The agent that grows with you

fix(gateway): a READY publication failure runs the cron/housekeeping shutdown tail

Teknium26180cd979c7
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.py
index 520f8939719..b3f0df1eaaa 100644
--- a/gateway/run_bootstrap.py
+++ b/gateway/run_bootstrap.py
@@ -792,23 +792,22 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
cron_stop, cron_provider, cron_thread, housekeeping_thread = (
_start_gateway_start_cron_and_housekeeping(runner))
-
- # READY only once adapters, cron and housekeeping run; missing systemd state just disables watchdog.
- from gateway.run_runtime import publish_gateway_runtime_ready
- publish_gateway_runtime_ready(runner)
- runner._start_systemd_watchdog()
- if idle_exit:
- from gateway.run_idle_exit import arm_unmanaged_idle_exit
- arm_unmanaged_idle_exit(runner)
- else:
- # A supervised/explicit gateway serves Desktop and the dashboard SPA, which call the
- # dashboard routes right away: mount them now, after READY. A client's own idle-exit
- # gateway (chat -q, TUI) only uses the native WebSocket; it mounts on first request.
- from gateway.run_api import warm_gateway_dashboard
- warm_gateway_dashboard(runner)
-
- from gateway.run_runtime import wait_gateway_runtime
+ # From here every exit (a READY publication that raises included) runs the shutdown tail,
+ # which stops the cron ticker and housekeeping before the adapters they deliver through close.
+ from gateway.run_runtime import publish_gateway_runtime_ready, wait_gateway_runtime
try:
+ # READY only once adapters, cron and housekeeping run; missing systemd state just disables watchdog.
+ publish_gateway_runtime_ready(runner)
+ runner._start_systemd_watchdog()
+ if idle_exit:
+ from gateway.run_idle_exit import arm_unmanaged_idle_exit
+ arm_unmanaged_idle_exit(runner)
+ else:
+ # A supervised/explicit gateway serves Desktop and the dashboard SPA, which call the
+ # dashboard routes right away: mount them now, after READY. A client's own idle-exit
+ # gateway (chat -q, TUI) only uses the native WebSocket; it mounts on first request.
+ from gateway.run_api import warm_gateway_dashboard
+ warm_gateway_dashboard(runner)
await wait_gateway_runtime(runner)
finally:
try:
diff --git a/tests/gateway/test_bootstrap_early_exit_teardown.py b/tests/gateway/test_bootstrap_early_exit_teardown.py
new file mode 100644
index 00000000000..96afd896dbb
--- /dev/null
+++ b/tests/gateway/test_bootstrap_early_exit_teardown.py
@@ -0,0 +1,39 @@
+"""start_gateway exits that never reach running mode must not look like a clean shutdown."""
+import asyncio
+import threading
+
+import pytest
+
+
+async def test_ready_publication_failure_stops_cron_and_housekeeping(monkeypatch):
+ from gateway import run_bootstrap, run_runtime
+ from gateway.config import GatewayConfig
+ from hermes_constants import get_hermes_home
+
+ get_hermes_home().chmod(0o700)
+ stopped = threading.Event()
+ threads = []
+
+ def start_owned_threads(runner):
+ for _ in range(2):
+ thread = threading.Thread(target=stopped.wait, daemon=True)
+ thread.start()
+ threads.append(thread)
+ return stopped, None, *threads
+
+ def refuse_ready(runner):
+ raise RuntimeError('gateway stopped before session API readiness')
+
+ monkeypatch.setattr(run_bootstrap, '_start_gateway_start_cron_and_housekeeping', start_owned_threads)
+ monkeypatch.setattr(run_runtime, 'publish_gateway_runtime_ready', refuse_ready)
+ try:
+ with pytest.raises(RuntimeError, match='before session API readiness'):
+ await asyncio.wait_for(run_bootstrap.start_gateway(GatewayConfig()), 30)
+ assert threads
+ assert stopped.is_set(), 'READY failure left the cron ticker running'
+ assert all(not thread.is_alive() for thread in threads)
+ finally:
+ stopped.set()
+ for thread in threads:
+ thread.join(timeout=2)