The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): reap PTY sessions whose child process died without EOF (#76759)
webtecnicaf33614102212
diff --git a/hermes_cli/pty_session.py b/hermes_cli/pty_session.pyindex c10ebccb3db..cc62f2f4c27 100644--- a/hermes_cli/pty_session.py+++ b/hermes_cli/pty_session.py@@ -156,6 +156,22 @@ class RegistryFull(Exception):super().__init__(message)+def _bridge_alive(s: "PtySession") -> bool:+ """True when the session's child process is still running.++ Duck-typed: fake bridges used in tests may not implement ``is_alive``;+ when the bridge doesn't expose it we conservatively treat the session as+ alive and rely on the EOF path (``alive = False``) instead.+ """+ is_alive = getattr(s.bridge, "is_alive", None)+ if is_alive is None:+ return True+ try:+ return bool(is_alive())+ except Exception:+ return False++async def run_reaper(registry: "PtySessionRegistry", *, interval: float = 60.0) -> None:"""Periodically reap idle/dead keep-alive sessions. Cancelled on shutdown."""while True:@@ -234,7 +250,17 @@ class PtySessionRegistry:now = time.monotonic() if now is None else nowdoomed = [key for key, s in self._sessions.items()- if not s.alive or (not s.attached and s.last_detached_at is not None and (now - s.last_detached_at) > self._ttl)+ if not s.alive+ or (not s.attached and s.last_detached_at is not None and (now - s.last_detached_at) > self._ttl)+ # The drain task sets ``alive = False`` only when it observes+ # EOF on the PTY master. If the child was killed externally+ # (OOM killer, cgroup SIGKILL, manual kill) while a grandchild+ # still holds the PTY slave, or the drain task itself died, EOF+ # never arrives and the session would pin its bridge (fds) and+ # registry slot forever — each leaked TUI subprocess costs+ # ~150-250 MB RSS in the dashboard's cgroup (#76759).+ # Ask the bridge directly; the check is a cheap WNOHANG waitpid.+ or not _bridge_alive(s)]for key in doomed:# Reaps overlap (attach_or_spawn and the background reaper) and close()diff --git a/tests/hermes_cli/test_pty_session.py b/tests/hermes_cli/test_pty_session.pyindex 0dce3616b83..4198bab6a82 100644--- a/tests/hermes_cli/test_pty_session.py+++ b/tests/hermes_cli/test_pty_session.py@@ -32,6 +32,7 @@ class FakeBridge:self.write_result = write_resultself.closed = Falseself.resized = None+ self.dead = Falsedef read(self, timeout):if not self._chunks:@@ -49,6 +50,9 @@ class FakeBridge:def close(self):self.closed = True+ def is_alive(self):+ return not self.dead+class FakeWS:def __init__(self):@@ -454,3 +458,33 @@ async def test_close_other_sessions_removes_old_profile_session():# The displaced viewer gets the documented supersede code rather than going silent.assert old_ws.close_code == WS_CLOSE_SUPERSEDEDawait reg.close_all()+++async def test_reap_reaps_dead_process_even_when_attached():+ # Child killed externally (OOM killer / cgroup SIGKILL) while a grandchild+ # holds the PTY slave, so the drain never sees EOF: the session must still+ # be reaped, otherwise its bridge and registry slot leak forever (#76759).+ reg = make_registry(ttl=3600.0)+ b = FakeBridge([b"", b"", b""])+ s, _ = await reg.attach_or_spawn("tok", spawn=lambda: b)+ await s.attach(FakeWS())+ b.dead = True+ await reg.reap_idle()+ assert "tok" not in reg._sessions+ assert b.closed is True+ await reg.close_all()+++async def test_reap_keeps_live_attached_session():+ # A session with a live child and an attached viewer is never reaped,+ # even past the detached TTL.+ reg = make_registry(ttl=1.0)+ b = FakeBridge([b"", b""])+ s, _ = await reg.attach_or_spawn("tok", spawn=lambda: b)+ await s.attach(FakeWS())+ await reg.reap_idle(now=time.monotonic() + 10)+ assert "tok" in reg._sessions+ assert b.closed is False+ await reg.close_all()