The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: keep readiness and cleanup fenced across runtime shutdown
Teknium82ff0592d078
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex f33d9d16b55..1024f547a74 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -37,14 +37,14 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -try:listener.bind(address)listener.setblocking(False)+ # Loading the ASGI graph may fail too; it owns the same bound socket.+ if not config.loaded:+ config.load()+ config.loaded_app = GatewayRuntimeAPI(config.loaded_app, runner, web.app)except BaseException:listener.close()raise- # Wrap this server's ASGI graph, never the module-global app/router.- if not config.loaded:- config.load()- config.loaded_app = GatewayRuntimeAPI(config.loaded_app, runner, web.app)web.app.state.gateway_runner = runnerweb.app.state.session_authority = getattr(runner, "session_authority", None)web.app.state.bound_host = host@@ -112,6 +112,15 @@ class GatewayRuntimeAPI:returnif scope['type'] != 'websocket' or scope['path'] != '/api/ws':return await self.app(scope, receive, send)+ original_receive = receive++ async def receive_admitted():+ message = await original_receive()+ if descriptor['state'] != 'ready' or self.runner._draining:+ return {'type': 'websocket.disconnect', 'code': 1013}+ return message++ receive = receive_admittedfrom starlette.websockets import WebSocketfrom hermes_cli.web_server_chat import (_gateway_ws_ticket_from_subprotocol, _ws_request_is_allowed,diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.pyindex da15c382c47..b8675c87c65 100644--- a/gateway/run_bootstrap.py+++ b/gateway/run_bootstrap.py@@ -231,6 +231,8 @@ async def _start_gateway_start_control_socket(runner):payload.update({key: descriptor[key] for key in ("instance_id", "runtime_protocol", "authority_epoch", "state", "api_origin","served_profiles", "capabilities") if key in descriptor})+ if getattr(runner, '_draining', False):+ payload.update(state='draining', capabilities=[])payload["supervisor"] = {"manual": "none", "desktop": "none"}.get(payload.get("supervisor"), payload.get("supervisor", "none"))return payload@@ -596,15 +598,17 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =_planned_stop_watcher_stop, _planned_stop_watcher_thread, _signal_initiated_shutdown)finally:- if runner is not None:- from gateway.run_runtime import drain_gateway_runtime- await drain_gateway_runtime(runner)- # Startup may have opened adapters before an exception. The same- # stop path owns their writers and DB handles on every exit.- await runner.stop()- if _planned_stop_watcher_stop is not None:- _planned_stop_watcher_stop.set()- if _control_server is not None:- await _control_server.stop()- remove_pid_file()- release_gateway_runtime_lock()+ try:+ if runner is not None:+ from gateway.run_runtime import drain_gateway_runtime+ await drain_gateway_runtime(runner)+ # Startup may have opened adapters before an exception. The same+ # stop path owns their writers and DB handles on every exit.+ await runner.stop()+ finally:+ if _planned_stop_watcher_stop is not None:+ _planned_stop_watcher_stop.set()+ if _control_server is not None:+ await _control_server.stop()+ remove_pid_file()+ release_gateway_runtime_lock()diff --git a/tests/gateway/test_normal_runtime_boot.py b/tests/gateway/test_normal_runtime_boot.pyindex 4d5098dea04..92cf1b47f6d 100644--- a/tests/gateway/test_normal_runtime_boot.py+++ b/tests/gateway/test_normal_runtime_boot.py@@ -80,6 +80,10 @@ def test_normal_entrypoint_earns_authenticated_authority_readiness(tmp_path):log.seek(0)assert descriptor.get('state') == 'ready', (descriptor, log.read())assert descriptor['authority_epoch'] > 0+ import sqlite3+ with sqlite3.connect(f"file:{home / 'state.db'}?mode=ro", uri=True) as db:+ assert db.execute('SELECT epoch, instance_id FROM runtime_epoch').fetchone() == (+ descriptor['authority_epoch'], descriptor['instance_id'])assert descriptor['served_profiles'] == [{'profile_id': str(home), 'home': str(home)}]assert 'session-authority-v1' in descriptor['capabilities']asyncio.run(handshake(home, descriptor))