Skip to content

The agent that grows with you

fix(gateway): exit non-zero when cgroup_cleanup refuses a live-gateway reap

kshitijk4poorcc963778f119
diff --git a/gateway/cgroup_cleanup.py b/gateway/cgroup_cleanup.py
index 0b21d64ab15..01e03f69e8d 100644
--- a/gateway/cgroup_cleanup.py
+++ b/gateway/cgroup_cleanup.py
@@ -95,7 +95,7 @@ def _live_gateway_in_cgroup(cgroup_path: str) -> bool:
def reap_cgroup(cgroup_path: str | None = None) -> int:
"""SIGKILL every PID in the cgroup other than the caller. Returns the count killed.
- Refuses (returns 0, no signals) when a live gateway process is still in
+ Refuses (returns -1, no signals) when a live gateway process is still in
the cgroup — the reaper must never signal the live gateway, no matter
how it was invoked.
"""
@@ -110,7 +110,7 @@ def reap_cgroup(cgroup_path: str | None = None) -> int:
"the gateway process has exited.",
file=sys.stderr,
)
- return 0
+ return -1
killed = 0
for pid in _read_cgroup_pids(cgroup_path):
if pid == os.getpid():
@@ -133,8 +133,7 @@ def main() -> int:
file=sys.stderr,
)
return 1
- reap_cgroup()
- return 0
+ return 1 if reap_cgroup() < 0 else 0
if __name__ == "__main__":
diff --git a/tests/gateway/test_cgroup_cleanup.py b/tests/gateway/test_cgroup_cleanup.py
index 28497e595f4..86bd570ffc6 100644
--- a/tests/gateway/test_cgroup_cleanup.py
+++ b/tests/gateway/test_cgroup_cleanup.py
@@ -71,7 +71,8 @@ class TestLiveGatewayGuard:
# Regression: a live gateway PID still in cgroup.procs (gateway is PID 1
# in a plain container, a targeted reap of a running service, or an
# in-process `gateway restart` on a host without a service manager)
- # must abort the reap before any signal.
+ # must abort the reap before any signal — even under a systemd parent —
+ # and main() must report the refusal with a non-zero exit.
import gateway.status
cgroup_path = "/some.slice/some-gateway.service"
@@ -89,7 +90,10 @@ class TestLiveGatewayGuard:
pytest.fail("os.kill must not signal a cgroup holding a live gateway")
monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
- assert cgroup_cleanup.reap_cgroup(cgroup_path) == 0
+ assert cgroup_cleanup.reap_cgroup(cgroup_path) == -1
+ monkeypatch.setattr(cgroup_cleanup, "_parent_is_systemd", lambda: True)
+ monkeypatch.setattr(cgroup_cleanup, "_own_cgroup_path", lambda: cgroup_path)
+ assert cgroup_cleanup.main() == 1
def test_reap_proceeds_when_only_orphans_in_cgroup(self, monkeypatch):
# Allow-path contract: orphans with non-gateway command lines must