Skip to content

The agent that grows with you

fix(gateway): finish connection teardown when a subscribed session was deleted

teknium19f5c8b5e1a40
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 3be89c2eea6..c8b41c921a0 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -321,8 +321,12 @@ class AuthorityConnection:
params["prompt_id"], {"answer": params["answer"]}, kind="clarify")
async def close(self):
- for subscription in self.subscriptions.values():
- await self.authority.detach(self.actor, subscription)
+ # Deletion may already have evicted a subscribed session; its membership died
+ # with it, and one retired ID must not leave the others (or the transport) attached.
+ for session_id, subscription in list(self.subscriptions.items()):
+ live = self.authority.sessions.get(session_id)
+ if live is not None and subscription in live.subscribers:
+ await self.authority.detach(self.actor, subscription)
from gateway.session_local_migration import unbind_native_transport
unbind_native_transport(self.authority, self.actor)
self.subscriptions.clear()
diff --git a/tests/gateway/test_authority_connection.py b/tests/gateway/test_authority_connection.py
index 4b564c510aa..e719ca11a96 100644
--- a/tests/gateway/test_authority_connection.py
+++ b/tests/gateway/test_authority_connection.py
@@ -36,3 +36,38 @@ async def test_repeated_resume_and_close_preserve_only_live_memberships(tmp_path
assert not members and not authority.events
finally:
db.close()
+
+
[email protected]('deleted_first', [True, False])
+async def test_close_after_a_subscribed_session_was_deleted_releases_every_membership(tmp_path, deleted_first):
+ """Deletion evicts the live session before the viewer disconnects; teardown must still
+ release the other subscription and the transport instead of raising not_found."""
+ import tui_gateway.ws # noqa: F401 — the fanout drain thread imports it lazily during teardown
+ db = SessionDB(db_path=tmp_path / 'state.db')
+ try:
+ for sid in ('doomed', 'kept'):
+ db.create_session(sid, source='test')
+ epoch = begin_runtime_epoch(db, instance_id='test')
+ store = SimpleNamespace(_routing_db=db, retire_runtime_sessions=lambda ids: None)
+ runner = SimpleNamespace(_draining=False, session_store=store, _evict_cached_agent=lambda route: None)
+ authority = SessionAuthority(runner, profile_id='test', instance_id='test', db=db, epoch=epoch)
+ for sid in ('doomed', 'kept'):
+ authority.sessions[sid] = LiveSession(None, sid + '-route')
+ viewer = AuthorityConnection(authority, object(), {'user_id': 'human'})
+ order = ['doomed', 'kept'] if deleted_first else ['kept', 'doomed']
+ for sid in order:
+ reply = await viewer.dispatch({'id': 1, 'method': 'session.resume', 'params': {'session_id': sid}})
+ assert 'result' in reply, reply
+ kept_members = authority.sessions['kept'].subscribers
+ reply = await viewer.dispatch({'id': 2, 'method': 'session.mutate', 'params': {
+ 'session_id': 'doomed', 'request_id': 'delete', 'expected_revision': 0,
+ 'expected_generation': 0, 'operation': 'delete', 'payload': {}}})
+ assert reply['result']['deleted_ids'] == ['doomed'], reply
+ assert 'doomed' not in authority.sessions
+ await viewer.close()
+ assert not kept_members
+ assert not viewer.subscriptions
+ assert viewer.actor.transport_id not in authority.events
+ finally:
+ db.close()