Skip to content

The agent that grows with you

refactor(gateway): one abandon-capture helper and one keyed FIFO lock

Teknium912a484ddfd0
diff --git a/gateway/platforms/webhook_ingress.py b/gateway/platforms/webhook_ingress.py
index 73a22323362..761b4ed974b 100644
--- a/gateway/platforms/webhook_ingress.py
+++ b/gateway/platforms/webhook_ingress.py
@@ -138,15 +138,9 @@ async def _delivery_turn(key):
if not key[2]: # no provider id: admission mints one, nothing can overlap
yield
return
- entry = _deliveries.setdefault(key, [asyncio.Lock(), 0])
- entry[1] += 1
- try:
- async with entry[0]:
- yield
- finally:
- entry[1] -= 1
- if not entry[1]:
- del _deliveries[key]
+ from gateway.session_authority import keyed_turn
+ async with keyed_turn(_deliveries, key):
+ yield
async def _release_preparation(authority, payload):
@@ -163,14 +157,12 @@ async def _prepared(authority, event):
authorization as admission, so a refused sender raises here."""
import json
from gateway.session_envelope import prepare_native, restore_native
- from gateway.session_ingress_media import capture_lease, release_abandoned_capture
+ from gateway.session_ingress_media import abandon_capture, capture_lease
with capture_lease(authority) as scope:
try:
payload = await prepare_native(authority.runner, event)
except BaseException:
- if not scope.abandoned:
- scope.abandoned = True
- await release_abandoned_capture(scope)
+ await abandon_capture(scope)
raise
source = restore_native(payload).source
identity = json.dumps([source.profile, source.platform.value, source.chat_id,
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 633ec706e55..911178b3558 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -89,6 +89,25 @@ def _log_drain_failure(task):
logging.getLogger(__name__).error('Session drain task died: %r', task.exception())
+
+async def keyed_turn(table, key, *, queued=None):
+ """One FIFO turn per *key*: ``table[key]`` is ``[asyncio.Lock(), holder + waiters]``, created on
+ first use and deleted when the last one leaves. asyncio's Lock is FIFO and a cancelled waiter gives
+ up only its own place. *queued* runs (before waiting) when an earlier turn holds or awaits the key."""
+ entry = table.setdefault(key, [asyncio.Lock(), 0])
+ entry[1] += 1
+ if queued is not None and entry[1] > 1:
+ queued()
+ try:
+ async with entry[0]:
+ yield
+ finally:
+ entry[1] -= 1
+ if not entry[1]:
+ del table[key]
+
+
class SessionAuthority:
def __init__(self, runner, *, profile_id, instance_id, db, epoch):
self.runner = runner
@@ -135,7 +154,7 @@ class SessionAuthority:
# Logical owner rotated away by the exhaustion auto-reset -> its session key: an admission
# that reserved it before the rotation commits on that key's current route instead.
self.rotated_owners = {}
- # Route key -> [lock, holder + waiters, stop fence] of the native admissions in flight (``_route_turn``).
+ # Route key -> [lock, holder + waiters] of the native admissions in flight (``_route_turn``, ``keyed_turn``).
self._route_turns = {}
# Per route: how many messaging /stop and /new fenced it (``fence_route``). Kept for the
# owner's life (one int per chat that ever got one): an input or command turn holding an older
@@ -409,7 +428,7 @@ class SessionAuthority:
async def admit_native(self, event):
"""Await current connector policy, then commit before ACK or scheduling execution."""
- from gateway.session_ingress_media import capture_lease, release_abandoned_capture
+ from gateway.session_ingress_media import abandon_capture, capture_lease
async with self._route_turn(event) as fence:
with capture_lease(self) as scope: # captured bytes are held until commit or refusal
try:
@@ -417,9 +436,8 @@ class SessionAuthority:
except BaseException:
# Before the shielded write starts, no row can own these bytes. A cancelled copy
# arranges its own completion cleanup in prepare_native; other failures clean here.
- if not scope.write_started and not scope.abandoned:
- scope.abandoned = True
- await release_abandoned_capture(scope)
+ if not scope.write_started:
+ await abandon_capture(scope)
raise
@contextlib.asynccontextmanager
@@ -438,25 +456,20 @@ class SessionAuthority:
if key is None:
yield None
return
- entry = self._route_turns.setdefault(key, [asyncio.Lock(), 0])
- entry[1] += 1
arrived = self.route_epoch(key)
def fence(_conn=None):
if self.route_epoch(key) != arrived:
raise RuntimeStoreError('stale_generation', field='route',
detail='a /stop or /new of this chat came after this message')
- if isinstance(ingress, asyncio.Future) and entry[1] > 1 and not ingress.done():
- ingress.set_result(None) # its FIFO position is fixed behind the earlier input
- try:
- async with entry[0]:
- if ingress is not None:
- fence()
- yield fence if ingress is not None else None
- finally:
- entry[1] -= 1
- if not entry[1]:
- del self._route_turns[key]
+
+ def queued(): # its FIFO position is fixed behind the earlier input
+ if isinstance(ingress, asyncio.Future) and not ingress.done():
+ ingress.set_result(None)
+ async with keyed_turn(self._route_turns, key, queued=queued):
+ if ingress is not None:
+ fence()
+ yield fence if ingress is not None else None
def route_epoch(self, route):
"""How many messaging /stop and /new fenced *route* (``fence_route``)."""
diff --git a/gateway/session_envelope.py b/gateway/session_envelope.py
index 0acc0e5e855..cbdc60b6f6b 100644
--- a/gateway/session_envelope.py
+++ b/gateway/session_envelope.py
@@ -180,7 +180,7 @@ async def prepare_native(runner, event):
# copied context carries the admission's capture lease.
media = None
if frozen.media_urls:
- from gateway.session_ingress_media import _lease_scope, release_abandoned_capture
+ from gateway.session_ingress_media import _lease_scope, abandon_capture, release_abandoned_capture
from gateway.session_runtime_workers import track_mutation
scope = _lease_scope.get()
work = asyncio.to_thread(capture_native_media, frozen.media_urls)
@@ -200,9 +200,7 @@ async def prepare_native(runner, event):
capture.add_done_callback(finished)
raise
except BaseException:
- if scope is not None and scope.authority is not None:
- scope.abandoned = True
- await release_abandoned_capture(scope)
+ await abandon_capture(scope)
raise
return _snapshot_native(runner, frozen, provenance, fresh_roles, encoded_source, media)
diff --git a/gateway/session_ingress_media.py b/gateway/session_ingress_media.py
index 3af05751163..7181e71beca 100644
--- a/gateway/session_ingress_media.py
+++ b/gateway/session_ingress_media.py
@@ -78,6 +78,15 @@ def drop_capture_lease(paths=None, *, scope=None):
del _leases[path]
+async def abandon_capture(scope):
+ """Mark *scope*'s capture abandoned and release it, once: no row can own these bytes. A no-op for no
+ scope, a scope without an authority, or one already abandoned (its release ran or is arranged)."""
+ if scope is None or scope.authority is None or scope.abandoned:
+ return
+ scope.abandoned = True
+ await release_abandoned_capture(scope)
+
+
async def release_abandoned_capture(scope):
"""Collect every path published by an abandoned worker, including after its caller left."""
if scope.authority is None:
diff --git a/tests/gateway/test_post_gateway_admission.py b/tests/gateway/test_post_gateway_admission.py
index e6b19a0e97b..15f8e49d52a 100644
--- a/tests/gateway/test_post_gateway_admission.py
+++ b/tests/gateway/test_post_gateway_admission.py
@@ -141,7 +141,7 @@ async def test_under_an_authority_the_hook_fires_once_at_ingress_never_at_execut
admitted.append(event.text)
return "admitted"
- monkeypatch.setattr(authorities, "active_authority", lambda _runner: object())
+ monkeypatch.setattr(authorities, "active_authority", lambda _runner: SimpleNamespace(route_epoch=lambda _route: 0))
monkeypatch.setattr(ingress, "admit_message", _admit)
assert await runner._handle_message(_event("eat me")) == "consumed"
diff --git a/tests/gateway/test_pre_gateway_dispatch.py b/tests/gateway/test_pre_gateway_dispatch.py
index 810f0740633..38579b80883 100644
--- a/tests/gateway/test_pre_gateway_dispatch.py
+++ b/tests/gateway/test_pre_gateway_dispatch.py
@@ -221,7 +221,7 @@ async def test_hook_fires_once_at_messaging_ingress_and_never_when_an_admission_
admitted = []
monkeypatch.setattr("hermes_cli.lifecycle.ainvoke_hook", hook)
- monkeypatch.setattr(authorities, "active_authority", lambda _runner: object())
+ monkeypatch.setattr(authorities, "active_authority", lambda _runner: SimpleNamespace(route_epoch=lambda _route: 0))
monkeypatch.setattr(ingress, "admit_message", admit)
runner, adapter = _make_runner(Platform.WHATSAPP)
runner._is_user_authorized_for_source = lambda _s: True