Skip to content

The agent that grows with you

fix(gateway): refuse restart fallback when gateway is service-managed under any unit name

Finn7635513f6b1f48c
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index c0c74c2a165..abd423bb28d 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5499,6 +5499,55 @@ def _restart_all_as_host(owner, system: bool) -> None:
run_gateway(verbose=0, replace=True)
+def _service_unit_owning_gateway(pid: int | None) -> str | None:
+ """systemd unit supervising gateway ``pid`` under ANY unit name, or None.
+
+ Canonical-unit checks miss pre-convention installs (``hermes.service``), so the restart
+ fallback SIGKILLed a service-managed gateway and spawned an unsupervised orphan in the
+ caller's cgroup while the unit flapped against the stolen lock (#126474). Reuses the
+ dashboard's MainPID-verified lookup: a bare ``.service`` cgroup alone (the caller itself
+ started under some unit) never proves ownership.
+ """
+ if not pid or pid <= 1:
+ return None
+ try:
+ from hermes_cli import main_dashboard as _dash
+ return _dash._get_systemd_service_for_pid(pid)
+ except Exception:
+ logger.debug("service-owner probe failed", exc_info=True)
+ return None
+
+
+def _refuse_restart_of_service_managed_gateway(pid: int | None) -> None:
+ """Refuse the manual restart fallback when a supervisor owns the gateway (any unit name).
+
+ The fallback's stop + in-process ``run_gateway`` would kill a live service-managed gateway
+ and stamp the CLI's PID into gateway.pid, wedging every supervisor respawn with "already
+ running". Restart through the unit instead — never from here.
+ """
+ unit = _service_unit_owning_gateway(pid)
+ if unit is None:
+ return
+ try:
+ from hermes_cli import main_dashboard as _dash
+ scope = _dash._extract_scope_from_cgroup(_dash._get_pid_cgroup_path(pid) or "")
+ except Exception:
+ scope = None
+ cmd = f"systemctl --user restart {unit}" if scope == "user" else f"sudo systemctl restart {unit}"
+ _print_lines(
+ "",
+ f"✗ Gateway (PID {pid}) is managed by systemd unit {unit}.",
+ " `hermes gateway restart` cannot restart it from here: the fallback would",
+ " kill a live gateway and spawn an unsupervised replacement in this shell's",
+ " cgroup while the unit keeps failing against the stolen lock.",
+ " Restart it through its unit instead:",
+ f" {cmd}",
+ " (Pre-convention `hermes.service` installs: `hermes gateway migrate-legacy`",
+ " removes the legacy unit so the canonical one can own the gateway.)",
+ )
+ sys.exit(1)
+
+
def _cmd_restart(args):
_refuse_from_inside_gateway("restart", "restart loops")
from hermes_cli.gateway_profile_lifecycle import profile_lifecycle
@@ -5574,6 +5623,7 @@ def _cmd_restart(args):
if supervised_pid and gateway_declares_external_supervisor(supervised_pid):
restart_externally_supervised_gateway(supervised_pid)
return
+ _refuse_restart_of_service_managed_gateway(supervised_pid)
if stop_profile_gateway():
print("✓ Stopped gateway for this profile")
diff --git a/tests/hermes_cli/test_gateway_restart_external_supervisor.py b/tests/hermes_cli/test_gateway_restart_external_supervisor.py
index 6d063535295..b6ead94df1e 100644
--- a/tests/hermes_cli/test_gateway_restart_external_supervisor.py
+++ b/tests/hermes_cli/test_gateway_restart_external_supervisor.py
@@ -97,3 +97,26 @@ def test_plain_manual_gateway_still_uses_stop_and_run(restart_calls, monkeypatch
_run_restart()
assert restart_calls["sigusr1"] is None, "no supervisor marker: the detached fallback is the restart"
assert restart_calls["started"], "a plain manually-run gateway must still be restarted in-process"
+
+
+def test_service_managed_gateway_refuses_fallback_takeover(restart_calls, monkeypatch, capsys):
+ """A gateway supervised under ANY unit name must not be taken over by the fallback (#126474).
+
+ Pre-convention ``hermes.service`` system unit, no ``--external-supervisor`` marker, control
+ socket unreachable while busy serving webhooks: the identify/argv probe missed it, so the
+ fallback SIGKILLed a live gateway and spawned an unsupervised orphan in the caller's cgroup
+ while the unit's ``Restart=`` flapped against the stolen lock for hours.
+ """
+ monkeypatch.setattr(gw, "_capture_gateway_argv",
+ lambda pid: ["/usr/bin/python", "-m", "hermes_cli.main", "gateway", "run"])
+ monkeypatch.setattr("hermes_cli.main_dashboard._get_systemd_service_for_pid",
+ lambda pid: "hermes.service" if pid == 4321 else None)
+ monkeypatch.setattr("hermes_cli.main_dashboard._get_pid_cgroup_path",
+ lambda pid: "/system.slice/hermes.service" if pid == 4321 else None)
+ with pytest.raises(SystemExit) as exc:
+ _run_restart()
+ assert exc.value.code == 1
+ assert not restart_calls["stopped"], "must not SIGTERM a service-managed gateway"
+ assert not restart_calls["started"], "must not spawn an unsupervised orphan"
+ out = capsys.readouterr().out
+ assert "hermes.service" in out and "systemctl restart" in out