Skip to content

The agent that grows with you

fix: retain gateway ownership through exit-state persistence

Tekniumd465ad0dbf63
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.py
index 812aa3d8530..3b16f92be14 100644
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@ -1802,12 +1802,9 @@ class GatewayShutdownMixin:
logger.info("Shutdown phase: SessionDB close done at +%.2fs", ctx.elapsed())
def _stop_persist_exit_state(self, ctx: "GatewayShutdownMixin._StopContext") -> None:
- """PID/lock release, clean-shutdown marker, restart markers, terminal runtime status."""
+ """Persist exit markers; process bootstrap releases ownership after writer drain."""
from gateway.run import _hermes_home, _planned_restart_notification_path, _shutdown_gateway_health_export
from utils import atomic_json_write
- from gateway.status import remove_pid_file, release_gateway_runtime_lock
- remove_pid_file()
- release_gateway_runtime_lock()
# Clean-shutdown marker skips suspend_recently_active() next boot; a timed-out drain left
# half-finished sessions, so no marker — the next startup suspends them.
if not ctx.timed_out:
diff --git a/tests/gateway/test_shutdown_runtime_ownership.py b/tests/gateway/test_shutdown_runtime_ownership.py
new file mode 100644
index 00000000000..4e786dac591
--- /dev/null
+++ b/tests/gateway/test_shutdown_runtime_ownership.py
@@ -0,0 +1,55 @@
+"""Exit-state persistence is not the end of canonical writer lifetime."""
+import os
+import subprocess
+import sys
+from types import SimpleNamespace
+
+import pytest
+
+
[email protected]('timed_out', [False, True])
+def test_exit_state_keeps_runtime_reserved_until_final_cleanup(tmp_path, monkeypatch, timed_out):
+ import gateway.run as run
+ from gateway.run_shutdown import GatewayShutdownMixin
+ from gateway.status import acquire_gateway_runtime_lock, release_gateway_runtime_lock, write_pid_file, remove_pid_file
+
+ home = tmp_path / 'runtime'
+ home.mkdir()
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ monkeypatch.setattr(run, '_hermes_home', home)
+ states = []
+ runner = SimpleNamespace(
+ _restart_requested=False, _exit_reason=None,
+ _update_runtime_status=lambda *args: states.append(args),
+ )
+ ctx = SimpleNamespace(timed_out=timed_out, active_agents={}, elapsed=lambda: 0)
+ probe = '''
+from gateway.status import acquire_gateway_runtime_lock, release_gateway_runtime_lock
+claimed = acquire_gateway_runtime_lock()
+print('CLAIMED', claimed)
+if claimed:
+ release_gateway_runtime_lock()
+'''
+
+ def contender():
+ result = subprocess.run(
+ [sys.executable, '-c', probe], cwd=os.getcwd(), env=os.environ.copy(),
+ stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=15,
+ )
+ assert result.returncode == 0, result.stderr
+ return result.stdout.strip()
+
+ assert acquire_gateway_runtime_lock()
+ write_pid_file()
+ try:
+ assert contender() == 'CLAIMED False'
+ GatewayShutdownMixin._stop_persist_exit_state(runner, ctx)
+ assert contender() == 'CLAIMED False', 'exit-state phase released ownership before writer drain'
+ assert (home / 'gateway.pid').exists()
+ assert (home / '.clean_shutdown').exists() is not timed_out
+ assert states == [('stopped', None)]
+ finally:
+ remove_pid_file()
+ release_gateway_runtime_lock()
+ assert contender() == 'CLAIMED True'