The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): whole-runtime settle logs late writers and finishes shutdown
Teknium0a739dc8b7f6
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.pyindex 619593d40c5..2f27edd006d 100644--- a/gateway/run_runtime.py+++ b/gateway/run_runtime.py@@ -396,7 +396,9 @@ async def settle_gateway_runtime(runner):A managed turn still running here gets its Stop first (its worker acknowledges or isterminated, and the admission settles interrupted or ``unknown``); a task that still misses- TURN_SETTLE_SECONDS retains ownership until its writer stops."""+ TURN_SETTLE_SECONDS is logged and left running for recovery. The stop sequence continues:+ the executor quiesce skips the DB close while a writer is live, and process exit releases+ the ownership lock, so raising here would only skip teardown and disarm the watchdog."""authorities = _authorities(runner)stop_managed_turns(runner)tasks = [task for authority in authorities for task in _authority_tasks(authority)]@@ -406,10 +408,15 @@ async def settle_gateway_runtime(runner):if not task.cancelled():task.exception()if pending:- raise TimeoutError('Runtime workers did not settle; ownership retained')+ logger.warning('%d authority task(s) did not settle within %.0fs; left for recovery',+ len(pending), TURN_SETTLE_SECONDS)from gateway.session_runtime_workers import join_authority_workfor authority in authorities:- await join_authority_work(authority, 0)+ try:+ await join_authority_work(authority, 0)+ except TimeoutError:+ logger.warning('Profile %s still has live writers at shutdown; its admissions stay for recovery',+ getattr(authority, 'profile_id', '?'))# Work settled above. ACP has no per-session destroy,# so the stop is the end of every ACP session nobody is viewing (#118216).from gateway.session_acp_lifecycle import end_idle_acp_sessionsdiff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.pyindex e70d697774c..c593c48d967 100644--- a/tests/gateway/test_runtime_worker_shutdown.py+++ b/tests/gateway/test_runtime_worker_shutdown.py@@ -84,3 +84,31 @@ async def test_profile_timeout_keeps_authority_until_physical_worker_exits(tmp_pfinally:release.set()await asyncio.gather(task, return_exceptions=True)+++async def test_whole_runtime_settle_logs_late_work_and_finishes_shutdown(monkeypatch):+ """A writer that outlives TURN_SETTLE_SECONDS must not abort the stop sequence: the rest of+ shutdown (tickets, API, adapters, flush, exit state) still runs and the watchdog stays armed."""+ late, wedged = asyncio.Event(), threading.Event()+ task = asyncio.create_task(late.wait())+ agent = SimpleNamespace(hard_interrupt=lambda message=None: None)+ live = SimpleNamespace(task=task, route='r', event_stream=SimpleNamespace(execution={}))+ authority = SimpleNamespace(sessions={'s': live}, _managed_workers={}, pending_stops={}, profile_id='p',+ _turn_workers={1: (SimpleNamespace(worker_done=wedged), [agent])})+ revoked, stopped = [], []+ runner = SimpleNamespace(session_authority=authority, _running_agents={},+ session_ticket_store=SimpleNamespace(revoke=lambda: revoked.append(True)),+ session_api=SimpleNamespace())+ authority.runner = runner+ async def stop_api(handle):+ stopped.append(handle)+ monkeypatch.setattr('gateway.run_api.stop_gateway_api', stop_api)+ monkeypatch.setattr(run_runtime, 'TURN_SETTLE_SECONDS', 0.05)+ try:+ await asyncio.wait_for(run_runtime.settle_gateway_runtime(runner), 5)+ assert revoked == [True] and stopped == [runner.session_api]+ assert not task.done(), 'late work is left for recovery, not cancelled into a false settlement'+ finally:+ late.set()+ await asyncio.gather(task, return_exceptions=True)