The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): resolve the orphan idle probe's target before opening a read
Kang6bb377a51d5c
diff --git a/gateway/session_viewer_close.py b/gateway/session_viewer_close.pyindex 3e052c7dcb0..3a6f2396c8e 100644--- a/gateway/session_viewer_close.py+++ b/gateway/session_viewer_close.py@@ -238,9 +238,14 @@ async def arm_detached_orphan_close(authority, session_id):def _idle_s(authority, session_id):from hermes_state_common import _sql_session_last_active+ # Resolve the target BEFORE opening a read context: ``_physical_target`` reads the receipt, and+ # a read that lands on the degraded path (non-WAL, or every read permit spent) waits on the+ # writer's non-reentrant lock. Nested inside this block it would wait on the lock this thread+ # already holds — a boot-time self-deadlock the watchdog can only answer by killing the gateway.+ target = _physical_target(authority, session_id)with authority.db._read_ctx() as conn:row = conn.execute(f'SELECT {_sql_session_last_active("s")} FROM sessions s WHERE s.id = ?',- (_physical_target(authority, session_id),)).fetchone()+ (target,)).fetchone()return time.time() - row[0] if row and row[0] else 0.0diff --git a/tests/gateway/test_local_session_orphan_end.py b/tests/gateway/test_local_session_orphan_end.pyindex 841cf4ba5a2..7da56de0457 100644--- a/tests/gateway/test_local_session_orphan_end.py+++ b/tests/gateway/test_local_session_orphan_end.py@@ -256,6 +256,42 @@ async def test_a_session_active_before_the_restart_is_kept_for_its_idle_classic_assert STARTUP_IDLE_S - 60 < delay <= STARTUP_IDLE_S+def test_the_startup_idle_probe_does_not_nest_its_read_context(tmp_path, monkeypatch):+ """``_idle_s`` runs on the boot thread, and a SessionDB read must not open inside another.++ A read that lands on the degraded path (no WAL: DELETE journal / NFS fallback) waits on the+ writer's non-reentrant lock. Resolving the session id inside ``_idle_s``'s own read context+ re-entered ``_read_ctx`` and waited on the lock this thread already held, so boot never+ reached the control socket and the liveness watchdog killed the gateway on every restart.+ Re-entry fails here instead of wedging the runner on that deadlock.+ """+ from contextlib import contextmanager+ from gateway.session_viewer_close import _idle_s+ from hermes_state import SessionDB++ db = SessionDB(db_path=tmp_path / 'state.db')+ db.create_session(session_id='s1', source='cli', model='m')+ original = db._read_ctx+ depth = 0++ @contextmanager+ def guarded_read_ctx():+ nonlocal depth+ assert depth == 0, '_idle_s opened a read context inside another read context'+ depth += 1+ try:+ with original() as conn:+ yield conn+ finally:+ depth -= 1++ monkeypatch.setattr(db, '_read_ctx', guarded_read_ctx)+ try:+ assert _idle_s(SimpleNamespace(db=db), 's1') >= 0+ finally:+ db.close()++@pytest.mark.asyncio@pytest.mark.parametrize('config, startup, detach', [('dashboard:\n ws_orphan_reap_grace_s: 0\n', [], []),