The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): an owner stop drops a one-shot result not yet written
unsupportedpastels43b6acbe37c5
diff --git a/gateway/session_llm_oneshot.py b/gateway/session_llm_oneshot.pyindex a5263ae7ced..447b78fbaef 100644--- a/gateway/session_llm_oneshot.py+++ b/gateway/session_llm_oneshot.py@@ -168,9 +168,11 @@ async def _answer(pool, transport, rid, call, check):raisefinally:stopping.cancel()- if not reply.done() or reply.cancelled():- # The owner stopped: a queued call was cancelled, a running call's late result is dropped.- reply.cancel()+ if pool.stopping.is_set() or not reply.done() or reply.cancelled():+ # The owner stopped first: a queued call was cancelled, a running call's late result (also one+ # that arrived while this task waited to wake) is dropped, its failure consumed unlogged.+ if not reply.cancel() and not reply.cancelled():+ reply.exception()frame = {'jsonrpc': '2.0', 'id': rid, 'error': {'code': 4001, 'message': 'runtime_draining', 'data': {'reason': 'runtime_draining'}}}else:diff --git a/tests/gateway/test_session_llm_oneshot_pool.py b/tests/gateway/test_session_llm_oneshot_pool.pyindex 60fad93a042..8f9684650b3 100644--- a/tests/gateway/test_session_llm_oneshot_pool.py+++ b/tests/gateway/test_session_llm_oneshot_pool.py@@ -4,6 +4,7 @@ A one-shot's model call is the one long blocking call the owner answers off thedispatch. It runs on the owner's own small pool, never the loop's default executor, and its answerlives only as long as the socket that asked for it and the owner that serves it."""import asyncio+import concurrent.futuresimport contextvarsimport gcimport threading@@ -170,6 +171,27 @@ def test_owner_shutdown_refuses_new_calls_and_cancels_the_queue():assert all(frame == {'jsonrpc': '2.0', 'id': frame['id'], 'error': _DRAINING} for frame in frames), frames+def test_owner_shutdown_drops_a_result_that_arrived_but_was_not_yet_written():+ """The call returned in its thread, but its answer task has not woken to write it when the owner+ stops: the stop wins, so the request is answered once as draining, never with the late result."""+ calls = _Calls()++ async def scenario():+ authority = SimpleNamespace()+ connection = _connection(authority)+ oneshot.DeferredReply(calls.work('late')).start(connection, 'late')+ await async_wait_until(lambda: calls.ran == ['late'])+ pool = authority._oneshot_pool+ running = list(pool.calls)+ calls.gate.set()+ # Block the loop until the thread has set the result: the answer task cannot run before stop.+ assert concurrent.futures.wait(running, timeout=10).not_done == set()+ oneshot.stop(authority)+ await async_wait_until(lambda: not pool.answers and not pool.calls)+ return connection.transport.frames+ assert asyncio.run(scenario()) == [{'jsonrpc': '2.0', 'id': 'late', 'error': _DRAINING}]++def test_owner_shutdown_answers_a_call_whose_reply_task_has_not_started_yet():calls = _Calls()