Skip to content

The agent that grows with you

fix(gateway): refuse cgroup_cleanup reaping under a non-systemd parent (prevents live-gateway self-kill)

jcjc8133dd6658d194
diff --git a/gateway/cgroup_cleanup.py b/gateway/cgroup_cleanup.py
index 8e34fc5a89b..28dfe4971a9 100644
--- a/gateway/cgroup_cleanup.py
+++ b/gateway/cgroup_cleanup.py
@@ -40,6 +40,24 @@ def _read_cgroup_pids(cgroup_path: str) -> list[int]:
return pids
+def _parent_is_systemd() -> bool:
+ """True when this process was spawned by a systemd manager (ExecStopPost etc.).
+
+ The reaper is safe only in that context: the gateway's main process is
+ already gone, and the cgroup holds orphans. Any other parent (an agent
+ terminal tool, a shell, a test) shares a *live* process's cgroup, so
+ reaping there SIGKILLs that process. Refuse loudly instead (issue:
+ 2026-09-29 engineering-gateway self-kill incident).
+ """
+ ppid = os.getppid()
+ if ppid == 1:
+ return True
+ try:
+ return Path(f"/proc/{ppid}/comm").read_text(encoding="utf-8").strip() == "systemd"
+ except OSError:
+ return False
+
+
def reap_cgroup(cgroup_path: str | None = None) -> int:
"""SIGKILL every PID in the cgroup other than the caller. Returns the count killed."""
cgroup_path = _own_cgroup_path() if cgroup_path is None else cgroup_path
@@ -58,6 +76,15 @@ def reap_cgroup(cgroup_path: str | None = None) -> int:
def main() -> int:
+ if not _parent_is_systemd():
+ print(
+ "cgroup_cleanup: refusing — not spawned by systemd. Running this "
+ "inside a live process's cgroup would SIGKILL that process. "
+ "Run it via the systemd unit's ExecStopPost, or call "
+ "reap_cgroup(cgroup_path) with an explicit stopped-service path.",
+ file=sys.stderr,
+ )
+ return 1
reap_cgroup()
return 0
diff --git a/tests/gateway/test_cgroup_cleanup.py b/tests/gateway/test_cgroup_cleanup.py
index 8544b6ccdb0..0163939fa4c 100644
--- a/tests/gateway/test_cgroup_cleanup.py
+++ b/tests/gateway/test_cgroup_cleanup.py
@@ -40,3 +40,66 @@ class TestReapCgroup:
monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
assert cgroup_cleanup.reap_cgroup(cgroup_path) == 0
+
+
+class TestMain:
+
+ def test_main_refuses_when_parent_not_systemd(self, tmp_path, monkeypatch):
+ # Parent comm is a shell (any non-systemd parent): main() must refuse
+ # and must never reach os.kill — the no-arg reaper shares the parent's
+ # live cgroup, so reaping there SIGKILLs that process.
+ comm = tmp_path / "comm"
+ comm.write_text("bash\n")
+ monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 4242)
+ monkeypatch.setattr(
+ cgroup_cleanup,
+ "Path",
+ lambda p: comm if p == "/proc/4242/comm" else Path(p),
+ )
+
+ def _explode(*_a, **_kw):
+ pytest.fail("os.kill must not be called when the parent is not systemd")
+
+ monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
+ assert cgroup_cleanup.main() == 1
+
+ def test_main_refuses_when_comm_unreadable(self, monkeypatch):
+ # Parent pid vanished (or /proc unreadable): fail closed — refuse.
+ monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 999999)
+
+ def _missing(*_a, **_kw):
+ raise OSError("no such process")
+
+ monkeypatch.setattr(
+ Path,
+ "read_text",
+ _missing,
+ raising=True,
+ )
+
+ def _explode(*_a, **_kw):
+ pytest.fail("os.kill must not be called when the parent is undeterminable")
+
+ monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
+ assert cgroup_cleanup.main() == 1
+
+ def test_main_proceeds_when_parent_is_systemd(self, tmp_path, monkeypatch):
+ # Simulates the ExecStopPost context: a real test child cannot have
+ # systemd as its parent, so the manager's comm is stood in with a fake
+ # /proc/<ppid>/comm. reap_cgroup is stubbed so the call never signals.
+ comm = tmp_path / "comm"
+ comm.write_text("systemd\n")
+ monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 4242)
+ monkeypatch.setattr(
+ cgroup_cleanup,
+ "Path",
+ lambda p: comm if p == "/proc/4242/comm" else Path(p),
+ )
+ calls: list[tuple] = []
+ monkeypatch.setattr(
+ cgroup_cleanup,
+ "reap_cgroup",
+ lambda *a, **kw: calls.append((a, kw)) or 0,
+ )
+ assert cgroup_cleanup.main() == 0
+ assert len(calls) == 1