The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process): grace host PID tree teardown
KoNit-K039165a3d4ce
diff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.pyindex 6703c313932..9641d001003 100644--- a/tests/tools/test_process_registry.py+++ b/tests/tools/test_process_registry.py@@ -1684,9 +1684,9 @@ class TestTerminateHostPidWindows:assert "/F" in captured["args"], "Force flag required for headless Chromium"class TestTerminateHostPidPosix:- """POSIX branch walks the tree via psutil and SIGTERMs children first."""+ """POSIX branch gives a managed parent its shutdown window first."""- def test_posix_walks_tree_and_terminates_children_then_parent(self, monkeypatch):+ def test_posix_terminates_parent_before_snapshot_descendants(self, monkeypatch):from tools import process_registry as primport psutil@@ -1711,16 +1711,15 @@ class TestTerminateHostPidPosix:terminate_order.append(self.pid)monkeypatch.setattr(psutil, "Process", _FakeParent)- # This test covers only the SIGTERM tree-walk ordering; disable the- # SIGKILL-escalation step (which would call psutil.wait_procs on the- # fakes) by setting the grace to 0.+ # A zero grace keeps this ordering probe deterministic while retaining+ # the configured no-SIGKILL behavior.monkeypatch.setattr(pr.ProcessRegistry, "_daemon_term_grace_seconds",staticmethod(lambda: 0.0))pr.ProcessRegistry._terminate_host_pid(12345)- assert terminate_order == [101, 102, 103, 12345], (- "Children must be terminated before the parent"+ assert terminate_order == [12345, 101, 102, 103], (+ "Parent must receive SIGTERM before any snapshot descendant")def test_posix_oserror_falls_back_to_os_kill(self, monkeypatch):diff --git a/tools/process_registry.py b/tools/process_registry.pyindex 2b0fd982092..a7bd4130e1d 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -818,9 +818,10 @@ class ProcessRegistry(ProcessCheckpointMixin):"""Terminate a host-visible PID and its descendants.``expected_start`` (kernel start time at spawn) is re-validated first: a mismatchor dead PID means the number was recycled onto a stranger and we refuse to touch- it — a leaked orphan beats tree-killing someone's browser. POSIX: psutil SIGTERMs- children before the parent (so trees aren't reparented to init and survive), then- SIGKILLs survivors after ``terminal.daemon_term_grace_seconds``. Windows:+ it — a leaked orphan beats tree-killing someone's browser. POSIX: snapshot descendants,+ SIGTERM the parent alone so it can perform an orderly shutdown, then clean up snapshot+ descendants that survive its grace window. Survivors are SIGKILLed after a second+ ``terminal.daemon_term_grace_seconds`` window. Windows:``taskkill /T /F`` (psutil's stale PPID links miss orphans there); ``os.kill``is the fallback."""if expected_start is not None and not cls._host_pid_is_ours(pid, expected_start):@@ -850,26 +851,52 @@ class ProcessRegistry(ProcessCheckpointMixin):except (OSError, PermissionError):_sigterm_quietly()return- # Snapshot the whole tree (children before parent) and SIGTERM each.+ # Snapshot before signalling: once the parent exits, psutil can no longer+ # reliably find children that it failed to reap.try:- targets = parent.children(recursive=True)+ descendants = parent.children(recursive=True)except gone:- targets = []- targets.append(parent)- for proc in targets:+ descendants = []++ # Let self-managing parents (notably Chromium/Electron) shut down their+ # tree before touching children. Killing their zygotes first can turn a+ # graceful browser shutdown into a crash dump.+ with suppress(gone):+ parent.terminate()++ grace = cls._daemon_term_grace_seconds()++ def _wait_for_exit(targets) -> None:+ if grace <= 0:+ return+ deadline = time.monotonic() + grace+ while time.monotonic() < deadline and any(cls._proc_alive(p) for p in targets):+ time.sleep(0.05)++ # Preserve descendants during the parent's configured shutdown window.+ _wait_for_exit([parent])++ # The snapshot is an anti-orphan guarantee: only descendants still alive+ # after the parent had its chance are asked to terminate themselves.+ remaining = descendants if grace <= 0 else [+ proc for proc in descendants if cls._proc_alive(proc)+ ]+ for proc in remaining:with suppress(gone):proc.terminate()++ # Preserve the existing SIGKILL escalation semantics for every owned+ # process that remains after its SIGTERM grace window. The parent is+ # included in case it ignored the first signal.+ targets = [parent, *remaining]# Escalate to SIGKILL for anything that ignored SIGTERM within the grace window.# ``psutil.wait_procs``' gone/alive partition is deliberately NOT trusted: it# reaps via ``Process.wait()`` and mis-partitions across zombie transitions in a# parent/child tree, leaving survivors un-killed. Re-probing every target is# deterministic.- grace = cls._daemon_term_grace_seconds()if grace <= 0:return- deadline = time.monotonic() + grace- while time.monotonic() < deadline and any(cls._proc_alive(_p) for _p in targets):- time.sleep(0.05)+ _wait_for_exit(targets)for proc in targets:with suppress(gone):if cls._proc_alive(proc):