Skip to content

The agent that grows with you

fix(gateway): managed worker reservation and loss writes leave the owner loop

Tekniumb7f08e92207f
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.py
index a71944560d8..09df305be50 100644
--- a/gateway/session_managed_worker.py
+++ b/gateway/session_managed_worker.py
@@ -3,6 +3,7 @@ import asyncio
from contextlib import contextmanager, suppress
from collections import deque
from dataclasses import asdict, replace
+from functools import partial
import json
import os
import queue
@@ -481,8 +482,10 @@ async def execute_managed(authority, ref, row, policy):
if worker.stop.is_set():
# Stopped before the child could receive controls: hello raced the latch; never bootstrap.
raise RuntimeStoreError('managed_worker_stopped')
- scope = reserve_admission_worker(authority, admission_id=row['admission_id'],
- process=process, principal_id=row['principal_id'], hello=hello)
+ # psutil + BEGIN IMMEDIATE: off-loop and retirement-tracked, like admission and claim.
+ from gateway.session_runtime_workers import tracked_write
+ scope = await tracked_write(authority, partial(reserve_admission_worker, authority,
+ admission_id=row['admission_id'], process=process, principal_id=row['principal_id'], hello=hello))
worker.worker = (scope['pid'], scope['birth'])
# The child reads nothing else until the exact reservation has committed.
frame = await asyncio.to_thread(_bootstrap, authority, ref, row, policy, scope)
@@ -534,7 +537,8 @@ async def execute_managed(authority, ref, row, policy):
return _interrupted_before_bootstrap(authority, row)
raise
from gateway.session_worker_reservation import lose_admission_worker
- lose_admission_worker(authority, row, scope)
+ from gateway.session_runtime_workers import tracked_write
+ await tracked_write(authority, partial(lose_admission_worker, authority, row, scope))
live = authority.sessions[ref.session_id]
with live.event_stream.lock:
live.controls.snapshot(ref.session_id, None)
diff --git a/tests/gateway/test_managed_worker_reservation_off_loop.py b/tests/gateway/test_managed_worker_reservation_off_loop.py
new file mode 100644
index 00000000000..41dcb599c83
--- /dev/null
+++ b/tests/gateway/test_managed_worker_reservation_off_loop.py
@@ -0,0 +1,103 @@
+"""The managed worker's reservation and loss writes run off the owner loop (B1-13): a state.db
+writer held by another process (up to _WRITE_PATIENCE_S) must not freeze every other session,
+timer, socket and Stop on the loop. Real child, real SQLite, a real competing writer."""
+import asyncio
+import sqlite3
+import subprocess
+import sys
+import threading
+import time
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.session_contract import Principal, SessionRef
+
+CHILD = """import json, os, sys, time, psutil
+me = psutil.Process()
+out = os.fdopen(os.dup(sys.stdout.fileno()), 'wb', buffering=0)
+def send(frame):
+ out.write((json.dumps(frame) + '\\n').encode())
+send({'type': 'hello', 'pid': me.pid, 'birth': me.create_time(), 'ancestors': [p.pid for p in me.parents()[:3]]})
+for line in sys.stdin.buffer:
+ frame = json.loads(line)
+ if 'version' in frame:
+ open(sys.argv[2], 'w').close()
+ time.sleep(.5)
+ send({'type': 'bogus'}) # an invalid frame: the owner revokes the assignment
+ elif frame == {'type': 'finish'}:
+ break
+"""
+HOLD_S = 3.0
+
+
[email protected]('held_during', ['reserve', 'lose'])
+def test_reservation_and_loss_writes_never_block_the_loop(tmp_path, monkeypatch, held_during):
+ from gateway import session_managed_worker as managed
+ from gateway.session_authority import LiveSession, SessionAuthority
+ from gateway.session_contract import Submission
+ from gateway.session_policy import build_policy
+ from hermes_state import SessionDB
+ from hermes_state_runtime import begin_runtime_epoch, claim_session_input
+
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ db = SessionDB(db_path=tmp_path / 'state.db')
+ db.create_session('s', source='test')
+ runner = SimpleNamespace(_draining=False, _cached_agent_for=lambda route: None,
+ _adapter_for_source=lambda source: None)
+ authority = SessionAuthority(runner, profile_id='owned', instance_id='i', db=db,
+ epoch=begin_runtime_epoch(db, instance_id='i'))
+ authority.sessions['s'] = LiveSession(SimpleNamespace(platform=None, user_id='human', chat_id='c'), 'route')
+ actor = Principal('human', 'owned', frozenset({'session:submit', 'session:control'}), 'cli')
+ ref = SessionRef('owned', 's')
+ monkeypatch.setattr(authority, '_schedule', lambda ref: None)
+ bootstrapped = tmp_path / 'bootstrapped'
+
+ class Child(subprocess.Popen):
+ def __init__(self, args, **kw):
+ super().__init__([sys.executable, '-c', CHILD, held_during, str(bootstrapped)], **kw)
+ monkeypatch.setattr(managed.subprocess, 'Popen', Child)
+ policy = build_policy({'cwd': str(tmp_path), 'model': 'm'}, {}, profile_terminal=False)
+ held = threading.Event()
+
+ def competing_writer():
+ if held_during == 'lose':
+ while not bootstrapped.exists():
+ time.sleep(.01)
+ rival = sqlite3.connect(tmp_path / 'state.db', isolation_level=None)
+ rival.execute('BEGIN IMMEDIATE')
+ held.set()
+ time.sleep(HOLD_S)
+ rival.execute('ROLLBACK')
+ rival.close()
+
+ async def scenario():
+ await authority.submit(actor, Submission('turn', ref, {'text': 'go'}, 'queue'))
+ row = claim_session_input(db, epoch=authority.epoch, session_id='s')
+ writer = threading.Thread(target=competing_writer, daemon=True)
+ writer.start()
+ if held_during == 'reserve':
+ assert await asyncio.to_thread(held.wait, 10)
+ ticks = []
+
+ async def ticker():
+ while True:
+ ticks.append(time.monotonic())
+ await asyncio.sleep(.05)
+ beat = asyncio.create_task(ticker())
+ try:
+ async with asyncio.timeout(60):
+ await managed.execute_managed(authority, ref, row, policy)
+ except managed.ManagedExecutionUnknown:
+ pass
+ finally:
+ beat.cancel()
+ await asyncio.to_thread(writer.join, 10)
+ return row, max(b - a for a, b in zip(ticks, ticks[1:]))
+ with db:
+ row, gap = asyncio.run(scenario())
+ status = db._read_one('SELECT status FROM session_admissions WHERE admission_id=?', (row['admission_id'],))[0]
+ assert held.is_set()
+ assert status == 'unknown' # the bogus frame still revokes the assignment once the writer frees
+ assert gap < HOLD_S / 2, f'the owner loop stalled {gap:.2f}s behind a held state.db writer'