Skip to content

The agent that grows with you

fix(gateway): use the runtime (run|restart) matcher in the cgroup live-gateway guard

jcjc81c45ff644f078
diff --git a/gateway/cgroup_cleanup.py b/gateway/cgroup_cleanup.py
index 5757bc44529..0b21d64ab15 100644
--- a/gateway/cgroup_cleanup.py
+++ b/gateway/cgroup_cleanup.py
@@ -73,14 +73,21 @@ def _live_gateway_in_cgroup(cgroup_path: str) -> bool:
line can no longer be read has already exited (or is a zombie) — exactly
what the reaper exists to clear — so only a readable, gateway-shaped
command line blocks.
+
+ Uses the *runtime* matcher (``run`` **or** ``restart``): on a host
+ without a service manager, ``hermes gateway restart`` runs
+ ``run_gateway()`` in-process, so the restart process is itself the live
+ runtime and must block a reap too. The strict ``run``-only matcher is
+ for lifecycle decisions (stop/replace); this cleanup scan is exactly
+ the use case its docstring carves out.
"""
- from gateway.status import _read_process_cmdline, looks_like_gateway_command_line
+ from gateway.status import _read_process_cmdline, looks_like_gateway_runtime_command_line
for pid in _read_cgroup_pids(cgroup_path):
if pid == os.getpid():
continue
cmdline = _read_process_cmdline(pid)
- if cmdline and looks_like_gateway_command_line(cmdline):
+ if cmdline and looks_like_gateway_runtime_command_line(cmdline):
return True
return False
diff --git a/tests/gateway/test_cgroup_cleanup.py b/tests/gateway/test_cgroup_cleanup.py
index 9650e92494d..77a4858223b 100644
--- a/tests/gateway/test_cgroup_cleanup.py
+++ b/tests/gateway/test_cgroup_cleanup.py
@@ -149,6 +149,30 @@ class TestLiveGatewayGuard:
monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
assert cgroup_cleanup.reap_cgroup("/some.slice/some-gateway.service") == 0
+ def test_reap_refuses_when_live_restart_in_cgroup(self, monkeypatch):
+ # Regression (restart form): on a host without a service manager,
+ # `hermes gateway restart` runs run_gateway() in-process — the
+ # restart process is the live runtime and must block a reap too.
+ # The guard must therefore use the *runtime* matcher (run|restart),
+ # not the strict run-only lifecycle matcher.
+ import gateway.status
+
+ restart_cmdline = "/opt/hermes/venv/bin/python -m hermes_cli.main gateway restart"
+ monkeypatch.setattr(
+ cgroup_cleanup, "_read_cgroup_pids", lambda _p: [777, os.getpid()]
+ )
+ monkeypatch.setattr(
+ gateway.status,
+ "_read_process_cmdline",
+ lambda pid: restart_cmdline if pid == 777 else None,
+ )
+
+ def _explode(*_a, **_kw):
+ pytest.fail("os.kill must not signal a cgroup holding a live restart process")
+
+ monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
+ assert cgroup_cleanup.reap_cgroup("/some.slice/some-gateway.service") == 0
+
def test_reap_proceeds_when_only_orphans_in_cgroup(self, monkeypatch):
# Allow-path contract: orphans with non-gateway command lines must
# still be reaped — the guard must not destroy the feature it secures.