fix(gateway): resolve the orphan idle probe's target before opening a read
_idle_s runs on the gateway's startup thread (recover_local_sessions -> arm_startup_orphan_closes) and resolved the physical session id inside its own SessionDB._read_ctx() block. _physical_target reads the local receipt, so that argument opened a second read context. A read that cannot use the WAL pool -- no WAL (DELETE journal / NFS fallback) or every read permit spent -- is served from the writer connection under SessionDB._lock, which is not reentrant: the nested read waited on the lock the same thread already held. Boot then never reached the control socket. Desktop clients gave up with "Gateway inaccessible: incomplete_control_response" / "Gateway starting: deadline", the loop-liveness watchdog dumped the stack and exited 75, and launchd restarted the gateway into the same wedge: a boot loop, not a slow start. Resolve the target first; the probe now opens exactly one read context. Test: test_the_startup_idle_probe_does_not_nest_its_read_context fails on main (the nested read) and passes here.
This commit is contained in:
@@ -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.0
|
||||
|
||||
|
||||
|
||||
@@ -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', [], []),
|
||||
|
||||
Reference in New Issue
Block a user