Skip to content

The agent that grows with you

fix: withdraw gateway readiness when the session listener exits

Teknium57ec27186f84
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 1024f547a74..d3c243729bf 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -86,7 +86,9 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -
async def stop_gateway_api(handle: GatewayAPIHandle) -> None:
"""Drain sockets without stopping the session authority or taking signals."""
handle.server.should_exit = True
- await asyncio.shield(handle.task)
+ # The bootstrap supervisor reports listener failure. Cleanup must still
+ # reach adapter/worker settlement when that listener raised or was cancelled.
+ await asyncio.shield(asyncio.gather(handle.task, return_exceptions=True))
class GatewayRuntimeAPI:
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.py
index b8675c87c65..d0b4f934b8a 100644
--- a/gateway/run_bootstrap.py
+++ b/gateway/run_bootstrap.py
@@ -591,7 +591,8 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
publish_gateway_runtime_ready(runner)
runner._start_systemd_watchdog()
- await runner.wait_for_shutdown()
+ 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,
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 325fb41c6a8..69c8e18e23b 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -48,6 +48,23 @@ def publish_gateway_runtime_ready(runner):
'session-authority-v1', 'durable-admission-v1', 'event-replay-v1'])
+async def wait_gateway_runtime(runner):
+ """A vanished interactive listener is fatal, not a healthy headless runtime."""
+ shutdown = asyncio.create_task(runner.wait_for_shutdown())
+ listener = runner.session_api.task
+ try:
+ done, _ = await asyncio.wait({shutdown, listener}, return_when=asyncio.FIRST_COMPLETED)
+ if listener in done and not runner._draining:
+ runner.session_runtime_descriptor.update(state='failed', capabilities=[])
+ error = None if listener.cancelled() else listener.exception()
+ raise RuntimeError('gateway session API stopped unexpectedly') from error
+ await shutdown
+ finally:
+ if not shutdown.done():
+ shutdown.cancel()
+ await asyncio.gather(shutdown, return_exceptions=True)
+
+
async def drain_gateway_runtime(runner):
"""Withdraw admission before any await; close sockets before DB teardown."""
from gateway.run_api import stop_gateway_api
diff --git a/tests/gateway/test_runtime_listener_supervision.py b/tests/gateway/test_runtime_listener_supervision.py
new file mode 100644
index 00000000000..9d80eaedde2
--- /dev/null
+++ b/tests/gateway/test_runtime_listener_supervision.py
@@ -0,0 +1,40 @@
+"""A dead session listener must never leave a ready gateway behind."""
+import asyncio
+
+import pytest
+
+
[email protected]('listener_failure', [False, True])
+async def test_runtime_wait_observes_listener_exit(tmp_path, listener_failure):
+ from gateway.config import GatewayConfig
+ from gateway.run import GatewayRunner
+ from gateway import run_runtime
+ from gateway.runtime_ownership import process_ownership
+ from hermes_constants import get_hermes_home
+
+ process_ownership.reserve([get_hermes_home()])
+ runner = GatewayRunner(GatewayConfig())
+ waiter = None
+ try:
+ await run_runtime.initialize_gateway_runtime(runner)
+ await run_runtime.start_gateway_runtime_api(runner)
+ assert await runner.start()
+ run_runtime.publish_gateway_runtime_ready(runner)
+ waiter = asyncio.create_task(run_runtime.wait_gateway_runtime(runner))
+ if listener_failure:
+ # Stop the actual listener independently of the runner lifecycle.
+ runner.session_api.server.should_exit = True
+ with pytest.raises(RuntimeError, match='session API stopped unexpectedly'):
+ await asyncio.wait_for(waiter, 10)
+ assert runner.session_runtime_descriptor['state'] != 'ready'
+ assert not runner.session_runtime_descriptor['capabilities']
+ else:
+ await runner.stop()
+ await asyncio.wait_for(waiter, 10)
+ finally:
+ if waiter is not None and not waiter.done():
+ waiter.cancel()
+ await asyncio.gather(waiter, return_exceptions=True)
+ await runner.stop()
+ process_ownership.close()