The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process): a released supervisor is reaped by a waiter thread, not left a zombie until the next registration
kshitijk4poore205a045c0c6
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex cf70be21c0d..aff07327e2c 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -821,9 +821,6 @@ _supervisor_lock = threading.Lock()# Groups the supervisor is reaping on our behalf; replayed verbatim on respawn so a respawn never# silently drops coverage for children that are still running._supervised_pgids: set = set()-# Released supervisors still exiting: polled (reaped) on the next registration change instead of-# waited on, so a release never blocks the caller (a foreground command released its group each run).-_retired_supervisors: list = []_SUPERVISOR_SCRIPT = Path(__file__).with_name("process_supervisor.sh")# Android (Termux) has no /bin/sh; its system shell is the same POSIX sh the script needs._POSIX_SH = "/bin/sh" if os.path.exists("/bin/sh") else "/system/bin/sh"@@ -961,7 +958,6 @@ def _update_supervised_pgids(verb: str, pgids) -> None:global _supervisorwith _supervisor_lock:- _retired_supervisors[:] = [old for old in _retired_supervisors if old.poll() is None]if verb == "register":_supervised_pgids.update(wanted)else:@@ -1007,7 +1003,9 @@ def _update_supervised_pgids(verb: str, pgids) -> None:proc.stdin.close()except (BrokenPipeError, ValueError, OSError):pass- # Reaped by the next registration change's poll(), not waited on here: Popen.wait polls- # with sleeps, which cost every release ~25 ms.- _retired_supervisors.append(proc)_supervisor = None+ # Reap it off the caller's thread: a timed Popen.wait polls with sleeps (~25 ms per+ # release), and leaving it unreaped keeps a zombie child that psutil walkers trip on. An+ # untimed wait() blocks in waitpid and returns the moment it exits.+ # health: allow HX012 -- the thread only waitpid()s a child that is exiting; no profile-scoped state+ threading.Thread(target=proc.wait, name="owner-death-supervisor-reaper", daemon=True).start()diff --git a/tests/hermes_cli/test_process_identity_supervisor.py b/tests/hermes_cli/test_process_identity_supervisor.pyindex 826334dc9b0..15c45bb3978 100644--- a/tests/hermes_cli/test_process_identity_supervisor.py+++ b/tests/hermes_cli/test_process_identity_supervisor.py@@ -371,7 +371,6 @@ def _reset_client_state():yieldprocess_identity._supervisor = Noneprocess_identity._supervised_pgids.clear()- process_identity._retired_supervisors.clear()@pytest.fixture@@ -445,14 +444,13 @@ def test_supervisor_is_released_once_nothing_is_left_to_reap(monkeypatch, all_grprocess_identity.release_process_groups([222])assert spawned[0].closed, "supervisor kept resident with nothing left to reap"- assert not getattr(spawned[0], "waited", False), "a release must not block on the exiting supervisor"+ wait_until(lambda: getattr(spawned[0], "waited", False), timeout=10,+ what="the released supervisor to be reaped (a zombie until the next Popen otherwise)")assert spawned[0].lines()[-1] == "unregister 222", "release happened before the last unregister was sent"assert process_identity._supervisor is None- spawned[0]._exited = Trueprocess_identity.own_process_groups([333])assert len(spawned) == 2 and spawned[1].lines() == ["register 333"]- assert process_identity._retired_supervisors == [], "the exited supervisor was never reaped"def test_supervisor_survives_the_real_eof_release():@@ -823,3 +821,20 @@ def test_the_supervisor_refuses_a_malformed_parent_pgid(parent):run = subprocess.run([process_identity._POSIX_SH, str(process_identity._SUPERVISOR_SCRIPT), parent],input="", capture_output=True, text=True, timeout=10, check=False)assert run.returncode == 2 and "bad parent pgid" in run.stderr+++def test_a_released_supervisor_leaves_no_zombie_child():+ """A foreground command registers and releases its group on every run; the exited supervisor+ must not linger as a zombie child (psutil walkers of Hermes's children raise ZombieProcess)."""+ import psutil++ child = subprocess.Popen(_VICTIM, start_new_session=True)+ try:+ process_identity.own_process_groups([child.pid])+ sup = process_identity._supervisor+ process_identity.release_process_groups([child.pid])+ wait_until(lambda: not psutil.pid_exists(sup.pid), timeout=10,+ what="the released supervisor to be reaped, not left a zombie")+ finally:+ _kill(child.pid)+ child.wait(timeout=10)