The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): queued-input cancellation writes off the owner event loop
Teknium9dadd0be5bdc
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 0cef8896879..9812c85c531 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -422,12 +422,23 @@ class SessionAuthority:async def cancel_queued(self, actor, ref, admission_id):before = await self.receipt(actor, ref, admission_id)+ from gateway.session_runtime_workers import track_mutation+ # Write off the shared loop; commit and observer settlement are one tracked task the+ # caller cannot cancel between (an exact retry still pays a left-over obligation).+ row = await asyncio.shield(track_mutation(self, self._commit_cancel(ref, admission_id, before)))+ # Media collection is its own write txn after the committed, settled cancellation: its+ # failure surfaces to the caller, and an exact retry collects again without re-settling.+ from gateway.session_ingress_media import release_admission_media+ await asyncio.to_thread(release_admission_media, self.db, admission_id)+ return self._receipt(row)++ async def _commit_cancel(self, ref, admission_id, before):if before.status == 'queued':# Owed from before the write: a commit that reports failure, or anything raising# between the commit and observer settlement, leaves it for an exact retry to pay.self.cancel_obligations.add(admission_id)try:- row = cancel_session_input(self.db, epoch=self.epoch, admission_id=admission_id)+ row = await asyncio.to_thread(cancel_session_input, self.db, epoch=self.epoch, admission_id=admission_id)except RuntimeStoreError:self.cancel_obligations.discard(admission_id) # a definite refusal: nothing committedraise@@ -437,11 +448,7 @@ class SessionAuthority:if row['status'] != 'queued':self.cancel_obligations.discard(admission_id)self._publish_pending(ref)- # Media collection is its own write txn after the committed, settled cancellation: its- # failure surfaces to the caller, and an exact retry collects again without re-settling.- from gateway.session_ingress_media import release_admission_media- release_admission_media(self.db, admission_id)- return self._receipt(row)+ return rowdef _settle_cancelled(self, ref, admission_id):# The only place a queued row becomes terminal: every observer kind that waits ondiff --git a/tests/gateway/test_settlement_lock_liveness.py b/tests/gateway/test_settlement_lock_liveness.pyindex 47709eb5aa9..b921f373388 100644--- a/tests/gateway/test_settlement_lock_liveness.py+++ b/tests/gateway/test_settlement_lock_liveness.py@@ -92,3 +92,32 @@ async def test_pending_publication_during_a_contended_settlement_keeps_idle_aftestart, complete = kinds.index(('message.start', None)), kinds.index(('message.complete', None))assert ('session.info', False) not in kinds[start:complete], kindsassert ('session.info', False) in kinds[complete:], kinds+++async def test_cancel_behind_a_held_writer_leaves_the_loop_free_and_still_settles_its_observer(+ tmp_path, monkeypatch):+ """``prompt.cancel`` / Delete on a queued card: its write waits on the writer, never on the loop."""+ db, authority = _authority(tmp_path, monkeypatch)+ monkeypatch.setattr(authority, '_schedule', lambda ref: None)+ with db:+ receipt = await authority.submit(ACTOR, Submission('queued', REF, {'text': 'queued'}, 'queue'))+ waiter = authority.waiters.setdefault(receipt.admission_id, asyncio.get_running_loop().create_future())+ released = threading.Event()+ writer = sqlite3.connect(str(tmp_path / 'state.db'), isolation_level=None, check_same_thread=False)+ writer.execute('BEGIN IMMEDIATE')++ def release():+ time.sleep(_HOLD_S)+ released.set()+ writer.rollback()+ writer.close()+ threading.Thread(target=release, daemon=True).start()+ ticks = []+ asyncio.get_running_loop().call_later(0.05, lambda: ticks.append(released.is_set()))+ cancel = asyncio.create_task(authority.cancel_queued(ACTOR, REF, receipt.admission_id))+ await asyncio.sleep(0.3)+ assert ticks == [False], 'an unrelated 50 ms callback waited for the held writer'+ cancelled = await asyncio.wait_for(cancel, 10)+ assert (cancelled.status, cancelled.outcome) == ('terminal', 'cancelled')+ assert waiter.done() and receipt.admission_id not in authority.cancel_obligations