Skip to content

The agent that grows with you

fix: kill verification waits for the tree to be reaped before reporting survivors (review follow-up)

teknium1a931b455a6a2
diff --git a/tests/tools/test_kill_verify_tree_death.py b/tests/tools/test_kill_verify_tree_death.py
index 85cb918ffc3..0acb6c35037 100644
--- a/tests/tools/test_kill_verify_tree_death.py
+++ b/tests/tools/test_kill_verify_tree_death.py
@@ -5,9 +5,13 @@ session: the live tree would become unmanageable (missing from the
background list while still holding resources).
"""
+import sys
import time
+from contextlib import suppress
from unittest.mock import MagicMock, patch
+import pytest
+
from tools.process_registry import ProcessRegistry, ProcessSession
@@ -98,3 +102,25 @@ def test_kill_with_dead_tree_still_reports_killed():
assert s.completion_reason == "killed"
assert s.id not in reg._running
assert s.id in reg._finished
+
+
[email protected](sys.platform == "win32", reason="POSIX signal escalation; Windows uses taskkill")
+def test_escalated_kill_of_sigterm_ignoring_child_reports_killed(tmp_path, monkeypatch):
+ """The #115490 scenario itself: a child that ignores SIGTERM is SIGKILLed after the grace
+ window, and the verification must give the kernel a moment to reap it — poll() right
+ after kill() still says alive, which turned every escalated kill into 'Kill incomplete'."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(ProcessRegistry, "_daemon_term_grace_seconds", staticmethod(lambda: 0.2))
+ monkeypatch.setattr(ProcessRegistry, "_write_checkpoint", lambda self: None)
+ reg = ProcessRegistry()
+ s = reg.spawn_local("trap '' TERM; sleep 30", cwd=str(tmp_path))
+ try:
+ time.sleep(0.3) # let bash install the trap
+ result = reg.kill_process(s.id)
+ finally:
+ with suppress(Exception):
+ s.process.kill()
+ s.process.wait(timeout=2)
+ assert result["status"] == "killed", result
+ assert s.completion_reason == "killed"
+ assert s.id in reg._finished
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 8c97e4a81b0..ed2f88a2eef 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -957,13 +957,27 @@ class ProcessRegistry(ProcessCheckpointMixin):
return []
return [c.pid for c in children if ProcessRegistry._proc_alive(c)]
+ # SIGKILL / taskkill are asynchronous: the kernel needs a scheduling tick to
+ # tear the process down and the parent must reap it before poll()/isalive()
+ # stop saying "alive". Verifying survivors in that window flagged every
+ # escalated kill as incomplete.
+ _KILL_SETTLE_SECONDS = 1.0
+
def _post_kill_survivors(self, session: "ProcessSession") -> List[int]:
- """Host PIDs still alive after the kill signals were delivered (#115490).
+ """Host PIDs still alive once the kill signals have had time to land (#115490).
Fail-closed: anything unverifiable counts as a survivor, so a kill
that leaves a live tree can never write a killed receipt. Sandbox
(env) sessions have no host-visible tree and are unverifiable by
design — they return no survivors, preserving existing behavior."""
+ deadline = time.monotonic() + self._KILL_SETTLE_SECONDS
+ while True:
+ survivors = self._probe_survivors(session)
+ if not survivors or time.monotonic() >= deadline:
+ return survivors
+ time.sleep(0.05)
+
+ def _probe_survivors(self, session: "ProcessSession") -> List[int]:
survivors: List[int] = []
proc = getattr(session, "process", None)
if proc is not None: