The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process): a thread that cannot start never escapes spawn_owned; the idle grace falls back to an immediate release
kshitijk4poor2730ebe7eb91
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.pyindex ed9b0dc2cdf..ac0f1a100fe 100644--- a/hermes_cli/process_identity.py+++ b/hermes_cli/process_identity.py@@ -886,8 +886,12 @@ def _watch_supervisor(proc) -> None:if restart:_start_supervisor_locked()- # health: allow HX012 -- the thread waitpid()s one child and touches only process-global supervisor state- threading.Thread(target=wait_then_restart, name="owner-death-supervisor-waiter", daemon=True).start()+ try:+ # health: allow HX012 -- the thread waitpid()s one child and touches only process-global supervisor state+ threading.Thread(target=wait_then_restart, name="owner-death-supervisor-waiter", daemon=True).start()+ except Exception:+ # The supervisor still works; it is only reaped at our exit and not restarted if killed.+ logger.debug("Could not start the owner-death supervisor's waiter", exc_info=True)# An idle supervisor (nothing registered) exits this long after the set last emptied, unless a new@@ -910,9 +914,16 @@ def _release_when_idle_locked(proc) -> None:with _supervisor_lock:_close_idle_supervisor_locked(proc)- _idle_release = threading.Timer(_SUPERVISOR_IDLE_EXIT_S, release)- _idle_release.daemon = True- _idle_release.start()+ try:+ _idle_release = threading.Timer(_SUPERVISOR_IDLE_EXIT_S, release)+ _idle_release.daemon = True+ _idle_release.start()+ except Exception:+ # No thread for the grace (thread limit, interpreter shutdown): release at once, as before the+ # grace existed. Losing the grace must never fail the live child this call is about.+ _idle_release = None+ logger.debug("Could not start the supervisor idle timer; releasing it now", exc_info=True)+ _close_idle_supervisor_locked(proc)def _close_idle_supervisor_locked(proc) -> None:diff --git a/tests/hermes_cli/test_process_identity_supervisor.py b/tests/hermes_cli/test_process_identity_supervisor.pyindex f86f34f5f13..26c40bacf41 100644--- a/tests/hermes_cli/test_process_identity_supervisor.py+++ b/tests/hermes_cli/test_process_identity_supervisor.py@@ -930,3 +930,24 @@ def test_an_idle_supervisor_stays_for_the_next_command_and_exits_after_the_graceassert process_identity._supervisor is first, "the second command spawned another supervisor"wait_until(lambda: process_identity._supervisor is None, timeout=10, what="the idle supervisor released")wait_until(lambda: first.returncode is not None, timeout=10, what="the idle supervisor to exit and be reaped")+++def test_a_process_that_cannot_start_a_thread_still_gets_its_child_and_releases_at_once(monkeypatch):+ """The idle grace and the waiter each start a thread. A failure there (thread limit, a patched+ Thread) must never escape spawn_owned after its Popen: the caller would lose the child's handle."""+ import threading as _threading++ monkeypatch.setattr(process_identity, "_SUPERVISOR_IDLE_EXIT_S", 30.0)+ monkeypatch.setattr(process_identity, "register_child", lambda *a, **k: True)++ def no_threads(*_a, **_kw):+ raise RuntimeError("can't start new thread")++ monkeypatch.setattr(_threading.Thread, "start", no_threads)+ child = process_identity.spawn_owned([sys.executable, "-c", "pass"], purpose="test")+ child.wait(timeout=10)+ supervisor = process_identity._supervisor+ process_identity.release_process_group_once_empty(child.pid, proc=child, timeout=5)+ assert process_identity._supervisor is None, "with no timer thread the supervisor is released at once"+ if supervisor is not None:+ supervisor.wait(timeout=10)diff --git a/tests/tools/test_process_registry_list_exit.py b/tests/tools/test_process_registry_list_exit.pyindex d4845a791c9..ba6230bbaa9 100644--- a/tests/tools/test_process_registry_list_exit.py+++ b/tests/tools/test_process_registry_list_exit.py@@ -106,6 +106,10 @@ def _probe(root):assert ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) == 0 # PR_SET_CHILD_SUBREAPERmodule._SYSTEMD_SCOPE_AVAILABLE = False # Own the test process tree, not a host service.+ # This process is the subreaper whose final wait collects every descendant; an idle owner-death+ # supervisor would hold that wait for its 30 s grace.+ from hermes_cli import process_identity+ process_identity._SUPERVISOR_IDLE_EXIT_S = 0.0registry = module.ProcessRegistry()sessions = []command = f"exec {shlex.quote(sys.executable)} {shlex.quote(__file__)}"