fix(gateway): reopen a finalized local row off the owner loop in the claim

_claim_next called reopen_local_session synchronously. On the first turn of
a finalized local session (TUI shutdown / disconnect / idle stamp) that is
db.reopen_session, a BEGIN IMMEDIATE write; with another writer holding
state.db's lock, every session on the owner loop froze for the writer wait.

The reopen now runs under asyncio.to_thread, still inside claim_gate +
mutation_lock and before the claim. A FIFO head that changed meanwhile is
already fenced by claim_session_input(head=first).

Test (red on r11-base: loop stalled 1.03s):
tests/gateway/test_claim_reopen_off_loop.py::test_reopening_a_finalized_local_row_behind_a_contended_writer_keeps_the_loop_live

Review: sprmn24 sprmn24-P2-residual-reopen
(cherry picked from commit c8362cacca2f1e139b604eeed11e8285b9c542c6)
This commit is contained in:
Teknium
2026-10-10 18:08:11 -07:00
parent b3744ab30a
commit b1546920a1
2 changed files with 86 additions and 2 deletions

View File

@@ -680,9 +680,9 @@ class SessionAuthority:
from gateway.session_operator import check_local_input
check_local_input(self, ref, first)
# The first real turn reopens a finalized row (#85303): mounts are reads, and
# SessionStore would route a stamped row as stale onto a FRESH session.
# SessionStore would route a stamped row as stale onto a FRESH session. A write: off-loop.
from gateway.session_local_recovery import reopen_local_session
reopen_local_session(self, ref)
await asyncio.to_thread(reopen_local_session, self, ref)
if first is not None and 'native_text_v1' in first['payload']:
from gateway.session_envelope import check_native_route
await check_native_route(self.runner, first['payload'], self.physical_target(ref), live.source,

View File

@@ -0,0 +1,84 @@
"""The claim phase's reopen of a finalized local row is a write: it runs off the owner loop."""
import asyncio
import sqlite3
import threading
import time
import pytest
@pytest.mark.asyncio
async def test_reopening_a_finalized_local_row_behind_a_contended_writer_keeps_the_loop_live(tmp_path, monkeypatch):
"""The first turn of a finalized local session reopens its row inside the claim. With another
writer holding state.db's lock at that moment, the reopen waits for it; the loop must not."""
from types import SimpleNamespace
from gateway import run, session_operator
from gateway.config import GatewayConfig
from gateway.session import SessionStore
from gateway.session_authority import SessionAuthority
from gateway.session_contract import Submission
from gateway.session_controls import AuthorityConnection
from gateway.session_local import create_local_session
import hermes_state_runtime as rt
monkeypatch.setattr(run, '_load_gateway_config', lambda *a: {})
store = SessionStore(tmp_path / 'sessions', GatewayConfig())
db = store._db
executed = []
async def handle(event):
executed.append(event.text)
return 'ok'
runner = SimpleNamespace(session_store=store, _session_db=db, adapters={}, _draining=False,
_evict_cached_agent=lambda route: None, _handle_message=handle,
_resolve_session_agent_runtime=lambda **k: ('frozen', {}))
runner._adapter_for_source = lambda source: runner.adapters.get(source.platform)
authority = SessionAuthority(runner, profile_id='owned', instance_id='owner', db=db,
epoch=rt.begin_runtime_epoch(db, instance_id='owner'))
owner = AuthorityConnection(authority, object(), {'user_id': 'human'})
ref = create_local_session(authority, owner.actor, dict(request_id='m', source='cli', cwd=str(tmp_path),
model='frozen', toolsets=[]))
db.end_session(ref.session_id, 'tui_shutdown')
held, release = threading.Event(), threading.Event()
def hold_writer():
other = sqlite3.connect(db.db_path, timeout=10)
other.execute('BEGIN IMMEDIATE')
held.set()
release.wait(1.0)
other.rollback()
other.close()
holder = threading.Thread(target=hold_writer)
check = session_operator.check_local_input
def check_then_contend(*args, **kwargs):
# The last preflight before the reopen: another process takes the writer lock now.
check(*args, **kwargs)
if not holder.is_alive() and not held.is_set():
holder.start()
held.wait(5)
monkeypatch.setattr(session_operator, 'check_local_input', check_then_contend)
gaps = []
async def ticker():
last = time.monotonic()
while True:
await asyncio.sleep(0.02)
now = time.monotonic()
gaps.append(now - last)
last = now
tick = asyncio.create_task(ticker())
try:
queued = await authority.submit(owner.actor, Submission('first', ref, {'text': 'FIRST'}, 'queue'))
async with asyncio.timeout(10):
while rt.get_session_admission(db, admission_id=queued.admission_id)['status'] != 'terminal':
await asyncio.sleep(0.02)
assert executed == ['FIRST']
assert db.get_session(ref.session_id)['end_reason'] is None
assert max(gaps) < 0.5, f'the owner loop stalled {max(gaps):.2f}s behind the reopen writer'
finally:
tick.cancel()
release.set()
if holder.is_alive():
holder.join(5)
await owner.close()
store.close_all_db_handles()