Skip to content

The agent that grows with you

perf(process): releasing the last owned group no longer waits on the exiting supervisor

kshitijk4poor2be70ed6aece
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index c5182de6775..cf70be21c0d 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -821,6 +821,9 @@ _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"
@@ -958,6 +961,7 @@ def _update_supervised_pgids(verb: str, pgids) -> None:
global _supervisor
with _supervisor_lock:
+ _retired_supervisors[:] = [old for old in _retired_supervisors if old.poll() is None]
if verb == "register":
_supervised_pgids.update(wanted)
else:
@@ -1003,9 +1007,7 @@ def _update_supervised_pgids(verb: str, pgids) -> None:
proc.stdin.close()
except (BrokenPipeError, ValueError, OSError):
pass
- # Reap it, or the exited supervisor stays a zombie until the next Popen in this process.
- try:
- proc.wait(timeout=5)
- except subprocess.TimeoutExpired:
- logger.warning("owner-death supervisor pid %s did not exit 5 s after release", proc.pid)
+ # 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
diff --git a/tests/hermes_cli/test_process_identity_supervisor.py b/tests/hermes_cli/test_process_identity_supervisor.py
index 0ef0713b21b..16b436705a3 100644
--- a/tests/hermes_cli/test_process_identity_supervisor.py
+++ b/tests/hermes_cli/test_process_identity_supervisor.py
@@ -371,6 +371,7 @@ def _reset_client_state():
yield
process_identity._supervisor = None
process_identity._supervised_pgids.clear()
+ process_identity._retired_supervisors.clear()
@pytest.fixture
@@ -444,12 +445,14 @@ def test_supervisor_is_released_once_nothing_is_left_to_reap(monkeypatch, all_gr
process_identity.release_process_groups([222])
assert spawned[0].closed, "supervisor kept resident with nothing left to reap"
- assert getattr(spawned[0], "waited", False), "released supervisor was never wait()ed -> zombie until the next Popen"
+ assert not getattr(spawned[0], "waited", False), "a release must not block on the exiting supervisor"
assert spawned[0].lines()[-1] == "unregister 222", "release happened before the last unregister was sent"
assert process_identity._supervisor is None
+ spawned[0]._exited = True
process_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():