Skip to content

The agent that grows with you

fix(gateway): make handoff recovery idempotent

fangliquan83b09ebd0a11
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 66d574ecf30..316a1457b87 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -4141,6 +4141,7 @@ def systemd_restart(system: bool = False):
f"⏳ {scope_label} service restarting gracefully (PID {pid}) — "
f"waiting up to {wait_budget:.0f}s for in-flight turns + drain..."
)
+ service_action = "restart"
if _graceful_restart_via_sigusr1(pid, wait_budget):
# Exit 75 transfers restart ownership to systemd. Observe that
# single replacement instead of issuing another restart that can
@@ -4153,6 +4154,8 @@ def systemd_restart(system: bool = False):
# A replacement may have started but not reached gateway runtime
# readiness before the wait expired. Never stop that generation.
props = _read_systemd_unit_properties(system=system)
+ if not props:
+ return
replacement_pid = _systemd_main_pid_from_props(props)
if (
props.get("ActiveState") in {"active", "activating", "reloading"}
@@ -4163,8 +4166,11 @@ def systemd_restart(system: bool = False):
print(
"⚠ Systemd did not relaunch the gateway after its graceful exit; "
- "forcing a service restart..."
+ "starting the inactive service..."
)
+ # ``start`` is intentionally idempotent: if a replacement appears
+ # after the snapshot, this must not stop that new generation.
+ service_action = "start"
else:
print(
f"⚠ Graceful restart did not complete within {int(wait_budget)}s; "
@@ -4178,7 +4184,9 @@ def systemd_restart(system: bool = False):
timeout=30,
)
try:
- _run_systemctl(["restart", svc], system=system, check=True, timeout=90)
+ _run_systemctl(
+ [service_action, svc], system=system, check=True, timeout=90
+ )
except subprocess.CalledProcessError as exc:
if _systemd_error_indicates_start_limit(
exc
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 1e38d90786f..35ea93cde23 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -789,7 +789,7 @@ class TestGatewaySystemServiceRouting:
gateway_cli.systemd_restart()
- assert [call[0][0] for call in calls] == ["reset-failed", "restart"]
+ assert [call[0][0] for call in calls] == ["reset-failed", "start"]
assert "did not relaunch" in capsys.readouterr().out
def test_systemd_restart_does_not_force_an_unready_replacement(self, monkeypatch):
@@ -819,6 +819,35 @@ class TestGatewaySystemServiceRouting:
assert calls == []
+ def test_systemd_restart_does_not_recover_when_handoff_state_is_unknown(
+ self, monkeypatch
+ ):
+ calls = []
+
+ monkeypatch.setattr(gateway_cli, "_select_systemd_scope", lambda system=False: False)
+ monkeypatch.setattr(gateway_cli, "_require_service_installed", lambda action, system=False: None)
+ monkeypatch.setattr(gateway_cli, "_preflight_user_systemd", lambda **kwargs: None)
+ monkeypatch.setattr(gateway_cli, "refresh_systemd_unit_if_needed", lambda system=False: None)
+ monkeypatch.setattr(gateway_cli, "_get_restart_exit_wait_budget", lambda: 27.0)
+ monkeypatch.setattr("gateway.status.get_running_pid", lambda: 654)
+ monkeypatch.setattr(gateway_cli, "_graceful_restart_via_sigusr1", lambda pid, timeout: True)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_wait_for_systemd_service_restart",
+ lambda system=False, previous_pid=None: False,
+ )
+ monkeypatch.setattr(gateway_cli, "_systemd_service_is_start_limited", lambda system=False: False)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_read_systemd_unit_properties",
+ lambda system=False, properties=None: {},
+ )
+ monkeypatch.setattr(gateway_cli, "_run_systemctl", lambda args, **kwargs: calls.append(args))
+
+ gateway_cli.systemd_restart()
+
+ assert calls == []
+
def test_systemd_restart_wait_timeout_includes_supervisor_budgets(self, monkeypatch):
monkeypatch.setattr(
gateway_cli,