Skip to content

The agent that grows with you

fix(gateway): reap cgroup PIDs from a fresh read taken after the live-gateway guard

kshitijk4poor99f1b9afe2d7
diff --git a/gateway/cgroup_cleanup.py b/gateway/cgroup_cleanup.py
index 1230969e51a..755f4b6ba7f 100644
--- a/gateway/cgroup_cleanup.py
+++ b/gateway/cgroup_cleanup.py
@@ -97,7 +97,12 @@ def reap_cgroup(cgroup_path: str | None = None) -> int | None:
)
return None
killed = 0
- for pid in others:
+ # Kill from a fresh read: the guard above can take seconds (status import,
+ # per-PID cmdline/ps fallback), so `others` may hold exited/reused PIDs and
+ # miss orphans spawned meanwhile.
+ for pid in _read_cgroup_pids(cgroup_path):
+ if pid == me:
+ continue
try:
os.kill(pid, signal.SIGKILL) # windows-footgun: ok — Linux-only (reads /proc, /sys/fs/cgroup; runs from a systemd unit)
killed += 1
diff --git a/tests/gateway/test_cgroup_cleanup.py b/tests/gateway/test_cgroup_cleanup.py
index 18180b34b3c..65220768a29 100644
--- a/tests/gateway/test_cgroup_cleanup.py
+++ b/tests/gateway/test_cgroup_cleanup.py
@@ -102,3 +102,12 @@ class TestLiveGatewayGuard:
monkeypatch.setattr(cgroup_cleanup.os, "kill", lambda pid, sig: killed.append(pid))
assert cgroup_cleanup.reap_cgroup(cgroup_path) == 1
assert killed == [777]
+
+ # The kill set comes from a fresh cgroup.procs read taken after the
+ # (slow) guard: a PID that exited meanwhile is not signalled (it may be
+ # reused outside the cgroup) and an orphan spawned meanwhile is reaped.
+ reads = iter([[777, os.getpid()], [888, os.getpid()]])
+ monkeypatch.setattr(cgroup_cleanup, "_read_cgroup_pids", lambda _p: next(reads))
+ killed.clear()
+ assert cgroup_cleanup.reap_cgroup(cgroup_path) == 1
+ assert killed == [888]