The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a cancel during the producer start hook releases its typing and bracket
Victor Kyriazakosc49ecd58922b
diff --git a/gateway/session_producer_lifecycle.py b/gateway/session_producer_lifecycle.pyindex 62e04048656..308ae321e5c 100644--- a/gateway/session_producer_lifecycle.py+++ b/gateway/session_producer_lifecycle.py@@ -78,7 +78,15 @@ async def _open(authority, row, event):# None for a heartbeat or ``typing_indicator: false``, as on the adapter path.bracket.typing = adapter._start_typing_refresh(event, bracket.stop, bracket.metadata)if hooks:- await adapter._run_processing_hook('on_processing_start', event)+ try:+ await adapter._run_processing_hook('on_processing_start', event)+ except BaseException:+ # Cancelled before ``producer_turn``'s finally exists: release what was opened here.+ bracket.stop.set()+ if bracket.typing is not None:+ bracket.typing.cancel()+ turns.pop(row['admission_id'], None)+ raisereturn bracketdiff --git a/tests/gateway/relay/test_relay_native_turn_lifecycle.py b/tests/gateway/relay/test_relay_native_turn_lifecycle.pyindex 2e9fe28eb60..dcd63ff0ae9 100644--- a/tests/gateway/relay/test_relay_native_turn_lifecycle.py+++ b/tests/gateway/relay/test_relay_native_turn_lifecycle.py@@ -311,3 +311,58 @@ async def test_a_scheduled_wake_on_the_chat_shows_no_reaction_or_typing(tmp_path# A bracket would have reacted and refreshed typing when the wake started, before its reply.assert [a for a in _actions(outgoing) if a.get('op') == 'react'] == before_reactsassert len(_heartbeats(outgoing, 'D7')) == before_typing+++async def test_a_cancel_while_the_start_hook_runs_leaks_no_typing():+ """A drain cancelled while on_processing_start is awaited stops typing and drops the bracket."""+ from gateway.platforms.base import BasePlatformAdapter+ from gateway.session_producer_lifecycle import producer_turn++ class Adapter(BasePlatformAdapter):+ name = property(lambda self: 'fake')++ def __init__(self):+ self._typing_paused = set()+ self._status_text = {}+ self.config = type('C', (), {'typing_indicator': True})()+ self.started = asyncio.Event()++ async def send_typing(self, chat_id, metadata=None):+ return None++ async def stop_typing(self, chat_id, metadata=None):+ return None++ async def on_processing_start(self, event):+ self.started.set()+ await asyncio.Event().wait()++ async def on_processing_complete(self, event, outcome):+ return None++ async def get_chat_info(self, *a, **k): ...+ async def connect(self): ...+ async def disconnect(self): ...+ async def send(self, *a, **k): ...++ adapter = Adapter()+ source = type('S', (), {'chat_id': 'C1', 'thread_id': None, 'platform': 'slack', 'chat_type': 'dm',+ 'user_id': 'U', 'scope_id': None, 'profile': None})()+ event = type('E', (), {'source': source, 'message_id': 'm1', 'raw_message': None, 'internal': False,+ 'reply_anchor_override': None})()+ runner = type('R', (), {'_adapter_for_source': lambda self, s: adapter})()+ authority = type('A', (), {'runner': runner, 'producer_turns': {'adm': None}})()++ async def drain():+ async with producer_turn(authority, {'admission_id': 'adm'}, event, {}):+ pass++ task = asyncio.create_task(drain())+ await adapter.started.wait()+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task+ await async_wait_until(lambda: not _typing_refreshes(), timeout=2)+ assert authority.producer_turns == {}+