Skip to content

The agent that grows with you

refactor(pty): lifespans reopen the terminal registry through its own method

emozilla0a28c29274c3
diff --git a/hermes_cli/pty_session.py b/hermes_cli/pty_session.py
index b63ad100a71..7b030197574 100644
--- a/hermes_cli/pty_session.py
+++ b/hermes_cli/pty_session.py
@@ -561,6 +561,12 @@ class PtySessionRegistry:
task.add_done_callback(self._background_closes.discard)
return task
+ def reopen(self) -> None:
+ """Take spawns again after :meth:`close_all`: the registry is process-wide, and a lifespan can
+ start again in one process (the gateway falling back from the Webapp's listener to its
+ private one)."""
+ self._closed = False
+
async def close_all(self) -> None:
self._closed = True
# Wait for any admitted spawn before snapshotting shutdown ownership.
diff --git a/hermes_cli/web_server_app.py b/hermes_cli/web_server_app.py
index e87cb3207c6..09230ae377f 100644
--- a/hermes_cli/web_server_app.py
+++ b/hermes_cli/web_server_app.py
@@ -132,7 +132,7 @@ async def standalone_lifespan(app: "FastAPI"):
# Reap idle/dead keep-alive PTY sessions (30-min TTL). The registry is process-wide and
# close_all() refuses new spawns, so a re-entered lifespan reopens it first.
- PTY_REGISTRY._closed = False
+ PTY_REGISTRY.reopen()
pty_reaper_task = asyncio.create_task(run_reaper(PTY_REGISTRY))
# Periodic authenticated self-test feeding the ``dashboard`` component on /api/status.
selftest_task = asyncio.create_task(_dashboard_selftest_loop())
@@ -210,7 +210,7 @@ async def http_lifespan(app: FastAPI):
app.state.event_lock = asyncio.Lock()
app.state.pty_active_session_files = {}
app.state.chat_argv_lock = asyncio.Lock()
- PTY_REGISTRY._closed = False # a prior lifespan's close_all() refuses spawns until reopened
+ PTY_REGISTRY.reopen() # a prior lifespan's close_all() refuses spawns until reopened
tasks = [
asyncio.create_task(run_reaper(PTY_REGISTRY), name="gateway-api-pty-reaper"),
asyncio.create_task(_dashboard_selftest_loop(), name="gateway-api-selftest"),
diff --git a/tests/hermes_cli/test_pty_session.py b/tests/hermes_cli/test_pty_session.py
index 33f85be19be..20e09070e1f 100644
--- a/tests/hermes_cli/test_pty_session.py
+++ b/tests/hermes_cli/test_pty_session.py
@@ -636,6 +636,22 @@ async def test_concurrent_reap_idle_is_idempotent():
assert all(b.closed for b in bridges)
+async def test_a_closed_registry_refuses_spawns_until_reopened():
+ """``close_all`` (a lifespan shutting down) refuses new terminals, so none outlives it; the
+ process-wide registry takes spawns again once a new lifespan reopens it (the gateway falling back
+ from the Webapp's listener to its private one in the same process)."""
+ reg = make_registry()
+ await reg.close_all()
+ with pytest.raises(RegistryFull):
+ await reg.attach_or_spawn("tok", spawn=lambda: FakeBridge([]))
+
+ reg.reopen()
+ session, created = await reg.attach_or_spawn("tok", spawn=lambda: FakeBridge([b""]))
+ assert created and session.alive
+ await reg.close_all()
+
+
@pytest.mark.asyncio
async def test_close_all_survives_key_popped_by_concurrent_reap():
"""close_all snapshots keys, then awaits each close(); a reap that runs