Skip to content

The agent that grows with you

fix(process): a supervisor killed on its own is restarted at once, with every group replayed

kshitijk4poorcd2bac710082
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index 3531633b86e..34a5001f32c 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -861,7 +861,7 @@ def _spawn_supervisor():
# start_new_session=True is load-bearing: shutdown paths killpg this process's own group,
# which would kill the supervisor before it could reap anything. close_fds keeps the write
# end of every OTHER pipe we hold out of it, so it never delays another child's EOF.
- return subprocess.Popen(
+ proc = subprocess.Popen(
[_POSIX_SH, str(_SUPERVISOR_SCRIPT), str(os.getpgid(0))],
stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True, close_fds=True, text=True, encoding="utf-8")
@@ -870,6 +870,59 @@ def _spawn_supervisor():
# normally; only the ungraceful-exit safety net is lost.
logger.debug("Could not start the owner-death supervisor", exc_info=True)
return None
+ _watch_supervisor(proc)
+ return proc
+
+
+# A supervisor that dies sooner than this after starting cannot stay up (a broken install): the waiter
+# leaves it to the next registration change instead of restarting it in a loop.
+_SUPERVISOR_RESTART_MIN_UPTIME_S = 5.0
+
+
+def _watch_supervisor(proc) -> None:
+ """One waiter thread per supervisor, blocked in the untimed ``wait()`` (waitpid, no polling). It
+ reaps the supervisor the moment it exits, so no zombie child is left for psutil walkers of this
+ process's children to trip on. A supervisor that died on its own while groups are still registered
+ (``pkill -f hermes``, the OOM killer) is restarted and replayed at once, so coverage does not wait
+ for the next registration change; a released one (EOF with an empty set) is only reaped."""
+ started = time.monotonic()
+
+ def wait_then_restart() -> None:
+ global _supervisor
+ proc.wait()
+ with _supervisor_lock:
+ if _supervisor is not proc:
+ return # released, or already replaced after a broken pipe: only reaped
+ _supervisor = None
+ _prune_dead_supervised_pgids()
+ if not _supervised_pgids:
+ return
+ restart = time.monotonic() - started >= _SUPERVISOR_RESTART_MIN_UPTIME_S
+ logger.warning(
+ "owner-death supervisor exited (status %s) with %d process group(s) registered; %s",
+ proc.returncode, len(_supervised_pgids),
+ "restarting it" if restart else "it could not stay up, retrying at the next registration")
+ 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()
+
+
+def _start_supervisor_locked():
+ """Start a supervisor and replay every live registration to it; the caller holds
+ ``_supervisor_lock``. Returns it, or None when it cannot start or its pipe broke at once."""
+ global _supervisor
+ proc = _supervisor = _spawn_supervisor()
+ if proc is None:
+ return None
+ try:
+ proc.stdin.write("".join(f"register {pgid}\n" for pgid in _supervised_pgids))
+ proc.stdin.flush()
+ except (BrokenPipeError, ValueError, OSError):
+ _supervisor = None # its waiter reaps it; the next registration change retries
+ return None
+ return proc
def is_owner_death_supervisor(argv: Sequence[str]) -> bool:
@@ -976,14 +1029,6 @@ def spawn_owned(argv, *, purpose: str, **popen_kwargs) -> subprocess.Popen:
return proc
-def _reap_in_background(proc) -> None:
- """Reap an exiting supervisor off the caller's thread. A timed ``Popen.wait`` polls with sleeps
- (~27 ms per release on macOS), and leaving it unreaped keeps a zombie child that psutil walkers of this
- process's children 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()
-
-
def _update_supervised_pgids(verb: str, pgids) -> None:
"""Send ``register``/``unregister`` for ``pgids`` to this process's supervisor. Failures are
swallowed: losing the safety net must never fail a live child."""
@@ -1012,25 +1057,21 @@ def _update_supervised_pgids(verb: str, pgids) -> None:
# SET, not the verb: after a broken-pipe write dropped the supervisor with groups
# still registered, an unregister must still rebuild coverage for the survivors.
return
- proc = _spawn_supervisor()
- _supervisor = proc
- if proc is None:
- return
- # A fresh supervisor knows nothing: replay live coverage (already reflects this
- # call's mutation and the prune, so pruned groups never reach the replacement).
- payload = "".join(f"register {pgid}\n" for pgid in _supervised_pgids)
- else:
- payload = "".join(f"{verb} {pgid}\n" for pgid in wanted)
- payload += "".join(f"unregister {pgid}\n" for pgid in stale)
+ # A fresh supervisor knows nothing: replay live coverage (already reflects this call's
+ # mutation and the prune, so pruned groups never reach the replacement).
+ _start_supervisor_locked()
+ return
+ payload = "".join(f"{verb} {pgid}\n" for pgid in wanted)
+ payload += "".join(f"unregister {pgid}\n" for pgid in stale)
try:
proc.stdin.write(payload)
proc.stdin.flush()
except (BrokenPipeError, ValueError, OSError):
# It exited between poll() and write(). Drop it so the next call respawns and replays
- # from ``_supervised_pgids`` (the set, not the pipe, is the record of what to reap).
+ # from ``_supervised_pgids`` (the set, not the pipe, is the record of what to reap). Its
+ # waiter thread reaps it.
_supervisor = None
- _reap_in_background(proc)
return
if not _supervised_pgids:
@@ -1041,5 +1082,4 @@ def _update_supervised_pgids(verb: str, pgids) -> None:
proc.stdin.close()
except (BrokenPipeError, ValueError, OSError):
pass
- _supervisor = None
- _reap_in_background(proc)
+ _supervisor = None # its waiter thread reaps it once it exits
diff --git a/tests/hermes_cli/test_process_identity_supervisor.py b/tests/hermes_cli/test_process_identity_supervisor.py
index ed0e3875a0c..a3ae1e37576 100644
--- a/tests/hermes_cli/test_process_identity_supervisor.py
+++ b/tests/hermes_cli/test_process_identity_supervisor.py
@@ -446,9 +446,7 @@ 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"
- 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].waited_on is not threading.current_thread(), "the release blocked on the supervisor's exit"
+ assert not getattr(spawned[0], "waited", False), "the release blocked on the supervisor's exit"
assert spawned[0].lines()[-1] == "unregister 222", "release happened before the last unregister was sent"
assert process_identity._supervisor is None
@@ -540,9 +538,8 @@ def test_a_broken_pipe_never_propagates_into_a_live_child(monkeypatch, all_group
process_identity.own_process_groups([111]) # must not raise
# Dropped, so the next registration respawns instead of writing into a
- # pipe that is known to be dead; and reaped, so it is not left a zombie.
+ # pipe that is known to be dead (a real one is reaped by its own waiter thread).
assert process_identity._supervisor is None
- wait_until(lambda: getattr(spawned[0], "waited", False), timeout=10, what="the dead supervisor to be reaped")
def test_unregister_after_a_broken_pipe_rebuilds_coverage_for_survivors(monkeypatch, all_groups_alive):
@@ -843,3 +840,29 @@ def test_a_released_supervisor_leaves_no_zombie_child():
finally:
_kill(child.pid)
child.wait(timeout=10)
+
+
+def test_a_supervisor_killed_on_its_own_is_restarted_with_every_group_replayed(monkeypatch):
+ """``pkill -f hermes`` (or the OOM killer) can take the supervisor out while this process lives.
+ Its waiter restarts it at once and replays the registered groups, so they still die with us even
+ if no registration change ever comes; it is also reaped, never left a zombie."""
+ import psutil
+
+ monkeypatch.setattr(process_identity, "_SUPERVISOR_RESTART_MIN_UPTIME_S", 0.0)
+ child = subprocess.Popen(_VICTIM, start_new_session=True)
+ try:
+ process_identity.own_process_groups([child.pid])
+ first = process_identity._supervisor
+ first.kill()
+ wait_until(lambda: process_identity._supervisor not in (None, first), timeout=10,
+ what="a replacement supervisor started without any registration change")
+ second = process_identity._supervisor
+ wait_until(lambda: not psutil.pid_exists(first.pid), timeout=10, what="the killed supervisor reaped")
+ assert child.poll() is None, "restarting the supervisor touched a live group"
+
+ second.stdin.close() # the EOF our death sends: the replayed group must be reaped
+ wait_until(lambda: child.poll() is not None, timeout=15,
+ what="the group registered before the restart killed by the replacement")
+ finally:
+ _kill(child.pid)
+ child.wait(timeout=10)