The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): retirement signals turns before the hosted-room stop and joins both in one deadline
Teknium2b79f9a4094f
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.pyindex 0b5d345b698..440f5c97a07 100644--- a/gateway/run_runtime.py+++ b/gateway/run_runtime.py@@ -247,19 +247,28 @@ async def _retire_profile_authority(authority):Claims are refused from here on; every executing turn gets a cooperative Stop, and its sessiontask is joined (an in-process turn's task ends only after its executor thread returned and the- admission settled). False when a turn misses TURN_SETTLE_SECONDS: its thread may still call tools- and write history, so the caller must keep the profile's reservation and store handles."""+ admission settled). The hosted-room stop runs alongside that join under the same deadline: it+ never interrupts accepted turns and its room threads wait for the member turns they observe, so+ those turns must be signalled first or the room stop cannot finish. False when a turn or room+ thread misses TURN_SETTLE_SECONDS: it may still call tools and write history, so the+ caller must keep the profile's reservation and store handles."""from gateway.session_cron import unbind_owner- from gateway.session_runtime_workers import join_authority_work+ from gateway.session_runtime_workers import join_authority_work, stop_authority_work+ timeout = TURN_SETTLE_SECONDSauthority.retiring = True+ stop_authority_work(authority)service = getattr(authority, 'hosted_room_service', None)- if service is not None:- if await asyncio.to_thread(service.stop, timeout=5) is False:- return False+ rooms = None if service is None else asyncio.ensure_future(asyncio.to_thread(service.stop, timeout=timeout))try:- await join_authority_work(authority, TURN_SETTLE_SECONDS)+ await join_authority_work(authority, timeout)+ workers_stopped = Trueexcept TimeoutError:logger.error('Profile %s workers did not stop; ownership retained', authority.profile_id)+ workers_stopped = False+ rooms_stopped = rooms is None or await rooms is not False+ if not rooms_stopped:+ logger.error('Profile %s hosted rooms did not stop; ownership retained', authority.profile_id)+ if not (workers_stopped and rooms_stopped):return False# Idle drains and receipt watchers wait on admissions this profile will no longer run.rest = _authority_tasks(authority)diff --git a/tests/gateway/test_profile_retire_stops_turns.py b/tests/gateway/test_profile_retire_stops_turns.pyindex 67a55324cbc..a6566a96ab2 100644--- a/tests/gateway/test_profile_retire_stops_turns.py+++ b/tests/gateway/test_profile_retire_stops_turns.py@@ -95,3 +95,38 @@ async def test_unserve_stops_and_joins_the_running_turn_before_release(tmp_path,agent.release.set()await asyncio.sleep(.05)db.close()+++class _RoomService:+ """Like the hosted runtime's stop: never interrupts the accepted member turn, and its room+ thread cannot finish until that turn has returned."""++ def __init__(self, returned):+ self.returned = returned++ def stop(self, *, timeout):+ return self.returned.wait(min(timeout, 1.5))+++async def test_retire_stops_the_turn_a_hosted_room_stop_waits_for(tmp_path, monkeypatch):+ from gateway import session_finite+ agent = _Agent()+ db, authority, execute, returned = _authority(tmp_path, agent)+ authority.hosted_room_service = _RoomService(returned)+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)+ monkeypatch.setattr(run_runtime, 'TURN_SETTLE_SECONDS', 1.0, raising=False)+ monkeypatch.setattr('gateway.session_cron.unbind_owner', lambda authority: None)+ try:+ with db:+ await authority.submit(ACTOR, Submission(request_id='running', ref=REF, payload={'text': 'a'}, intent='queue'))+ await asyncio.wait_for(asyncio.to_thread(agent.started.wait, 5), 6)++ retired = await run_runtime._retire_profile_authority(authority)++ assert agent.stopped.is_set(), 'the room stop failed before the turn was ever told to stop'+ assert retired is True and returned.is_set()+ finally:+ agent.release.set()+ await asyncio.sleep(.05)+ db.close()