Skip to content

The agent that grows with you

fix(gateway): never let the marker write abort a CLI restart

Leegenuxdfa1171dd075
diff --git a/gateway/restart_notice.py b/gateway/restart_notice.py
index db12057e216..83574d1f2bb 100644
--- a/gateway/restart_notice.py
+++ b/gateway/restart_notice.py
@@ -11,13 +11,15 @@ The reader is ``gateway/run.py::_planned_restart_notification_path``; the roundt
from __future__ import annotations
-import contextlib
+import logging
import time
from pathlib import Path
from hermes_constants import get_process_hermes_home
from utils import atomic_json_write
+logger = logging.getLogger(__name__)
+
PLANNED_RESTART_MARKER_FILENAME = ".restart_pending.json"
@@ -29,11 +31,16 @@ def write_planned_restart_marker() -> None:
"""Record that the running gateway is going down FOR A RESTART; never blocks the restart.
Best-effort like ``gateway.status.write_planned_stop_marker``: an unwritable home costs the
- notice, not the restart.
+ notice, not the restart. Wider than ``OSError`` on purpose — a marker write that raises for any
+ other reason must still not leave the caller's ``stop()`` un-run.
"""
- with contextlib.suppress(OSError):
+ try:
atomic_json_write(
planned_restart_marker_path(),
{"requested_at": time.time(), "via_service": False, "detached": False},
indent=None,
)
+ except Exception:
+ logger.debug(
+ "Could not record the planned restart; home channels get no online notice", exc_info=True
+ )
diff --git a/tests/gateway/test_planned_restart_marker_cli.py b/tests/gateway/test_planned_restart_marker_cli.py
index 0fefddc9feb..af671d70b65 100644
--- a/tests/gateway/test_planned_restart_marker_cli.py
+++ b/tests/gateway/test_planned_restart_marker_cli.py
@@ -52,3 +52,53 @@ def test_windows_restart_stamps_the_marker_before_stopping(hermes_home, monkeypa
gw.restart()
assert seen["marker_at_stop"] is True
+
+
+def test_failed_restart_still_leaves_the_marker(hermes_home, monkeypatch):
+ """A restart that never comes back must not swallow the notice.
+
+ The marker is what tells the *next* boot that a return is owed, so it has to survive a
+ ``start()`` that raises — otherwise a crash-looping gateway goes silent exactly when the user
+ most needs to know it is down.
+ """
+ from hermes_cli import gateway as gw_facade
+ from hermes_cli import gateway_windows as gw
+
+ def not_ready(timeout_s=15.0):
+ raise RuntimeError("Gateway restart did not produce a running gateway process.")
+
+ monkeypatch.setattr(gw, "_assert_windows", lambda: None)
+ monkeypatch.setattr(gw, "stop", lambda: None)
+ monkeypatch.setattr(gw, "_wait_for_gateway_absent", lambda timeout_s=30.0: True)
+ monkeypatch.setattr(gw_facade, "_wait_for_api_server_port_free", lambda: None)
+ monkeypatch.setattr(gw, "start", lambda: None)
+ monkeypatch.setattr(gw, "_wait_for_gateway_ready", not_ready)
+
+ with pytest.raises(RuntimeError):
+ gw.restart()
+
+ assert (hermes_home / MARKER).exists()
+
+
+def test_an_unwritable_marker_does_not_block_the_restart(hermes_home, monkeypatch):
+ """Losing the notice is acceptable; losing the restart is not."""
+ from gateway import restart_notice
+ from hermes_cli import gateway as gw_facade
+ from hermes_cli import gateway_windows as gw
+
+ stopped: list[str] = []
+
+ def boom(*_a, **_kw):
+ raise RuntimeError("disk full")
+
+ monkeypatch.setattr(restart_notice, "atomic_json_write", boom)
+ monkeypatch.setattr(gw, "_assert_windows", lambda: None)
+ monkeypatch.setattr(gw, "stop", lambda: stopped.append("stop"))
+ monkeypatch.setattr(gw, "_wait_for_gateway_absent", lambda timeout_s=30.0: True)
+ monkeypatch.setattr(gw_facade, "_wait_for_api_server_port_free", lambda: None)
+ monkeypatch.setattr(gw, "start", lambda: stopped.append("start"))
+ monkeypatch.setattr(gw, "_wait_for_gateway_ready", lambda timeout_s=15.0: True)
+
+ gw.restart()
+
+ assert stopped == ["stop", "start"]