Skip to content

The agent that grows with you

fix: stop bootstrap background workers after listener failure

Teknium6716636ffc96
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.py
index d0b4f934b8a..713d8c84eee 100644
--- a/gateway/run_bootstrap.py
+++ b/gateway/run_bootstrap.py
@@ -592,11 +592,16 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
runner._start_systemd_watchdog()
from gateway.run_runtime import wait_gateway_runtime
- await wait_gateway_runtime(runner)
-
- return await _start_gateway_shutdown_tail(
- runner, _control_server, cron_stop, cron_provider, cron_thread, housekeeping_thread,
- _planned_stop_watcher_stop, _planned_stop_watcher_thread, _signal_initiated_shutdown)
+ try:
+ await wait_gateway_runtime(runner)
+ finally:
+ try:
+ await runner.stop()
+ finally:
+ verdict = await _start_gateway_shutdown_tail(
+ runner, _control_server, cron_stop, cron_provider, cron_thread, housekeeping_thread,
+ _planned_stop_watcher_stop, _planned_stop_watcher_thread, _signal_initiated_shutdown)
+ return verdict
finally:
try:
diff --git a/tests/gateway/test_runtime_listener_supervision.py b/tests/gateway/test_runtime_listener_supervision.py
index 9d80eaedde2..c8f3eea6c0f 100644
--- a/tests/gateway/test_runtime_listener_supervision.py
+++ b/tests/gateway/test_runtime_listener_supervision.py
@@ -38,3 +38,41 @@ async def test_runtime_wait_observes_listener_exit(tmp_path, listener_failure):
await asyncio.gather(waiter, return_exceptions=True)
await runner.stop()
process_ownership.close()
+
+
+async def test_listener_failure_stops_bootstrap_background_threads(monkeypatch):
+ import threading
+ 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
+
+ publish = run_runtime.publish_gateway_runtime_ready
+
+ def fail_after_ready(runner):
+ publish(runner)
+ runner.session_api.server.should_exit = True
+
+ monkeypatch.setattr(run_bootstrap, '_start_gateway_start_cron_and_housekeeping', start_owned_threads)
+ monkeypatch.setattr(run_runtime, 'publish_gateway_runtime_ready', fail_after_ready)
+ try:
+ with pytest.raises(RuntimeError, match='session API stopped unexpectedly'):
+ await asyncio.wait_for(run_bootstrap.start_gateway(GatewayConfig()), 20)
+ assert threads
+ assert stopped.is_set(), 'listener failure skipped background-writer shutdown'
+ assert all(not thread.is_alive() for thread in threads)
+ finally:
+ stopped.set()
+ for thread in threads:
+ thread.join(timeout=2)