Skip to content

The agent that grows with you

fix(gateway): Graph webhook admission commits off the owner loop

Tekniumc0a166f95e0c
diff --git a/gateway/platforms/msgraph_webhook.py b/gateway/platforms/msgraph_webhook.py
index ff0c7e2b9bf..c7015f0a1d2 100644
--- a/gateway/platforms/msgraph_webhook.py
+++ b/gateway/platforms/msgraph_webhook.py
@@ -337,7 +337,8 @@ class MSGraphWebhookAdapter(BasePlatformAdapter):
provenance = capture_provenance(authority.runner, event)
if provenance is None:
raise RuntimeStoreError('permission_denied')
- ref = authority.register(event.source)
+ # The routing reservation writes too: off the owner loop like the admission below.
+ ref = await asyncio.to_thread(authority.register, event.source)
route = authority.sessions[ref.session_id].route
source = event.source.to_dict()
source['is_bot'] = event.source.is_bot
@@ -345,10 +346,14 @@ class MSGraphWebhookAdapter(BasePlatformAdapter):
'timestamp': datetime.fromtimestamp(0, UTC).isoformat(),
'event': {'message_id': event.message_id},
'automation': {'identity': event.message_id, 'owner': ref.session_id}}
- row = admit_session_input(authority.db, epoch=authority.epoch,
- principal_id='msgraph:' + route, session_id=ref.session_id,
- request_id=event.message_id, payload={'text': event.text, 'native_text_v1': envelope})
- event._gateway_accepted = True
- authority._publish_pending(ref)
- authority._schedule(ref)
- return authority._receipt(row)
+ # Off the owner loop and in admission order (``tracked_write``), committed before the
+ # 202: a held SQLite writer must not freeze every session while Graph waits.
+ from functools import partial
+ from gateway.session_runtime_workers import tracked_write
+ return await tracked_write(authority, partial(
+ admit_session_input, authority.db, epoch=authority.epoch,
+ principal_id='msgraph:' + route, session_id=ref.session_id, request_id=event.message_id,
+ payload={'text': event.text, 'native_text_v1': envelope},
+ _authorize_write=authority._admission_gate()),
+ then=partial(authority._admitted, ref, event), ordered=True,
+ after=partial(authority._schedule_admitted, ref))
diff --git a/tests/gateway/test_ingress_admission_off_loop.py b/tests/gateway/test_ingress_admission_off_loop.py
new file mode 100644
index 00000000000..10107c6384f
--- /dev/null
+++ b/tests/gateway/test_ingress_admission_off_loop.py
@@ -0,0 +1,60 @@
+"""Ingress admissions commit off the owner loop: a SQLite writer held by another process must
+not freeze every session, timer and socket while a producer waits for its ACK (andrexibiza-21,
+JoaoMarcos44-R7). One row per ingress admission site this lane owns (see SWEEP_owner-loop-writes)."""
+import asyncio
+import sqlite3
+import threading
+import time
+
+import pytest
+
+
+async def _msgraph(tmp_path):
+ from gateway.config import Platform, PlatformConfig
+ from gateway.platforms.msgraph_webhook import MSGraphWebhookAdapter
+ from gateway.run import GatewayRunner
+ from gateway.session_authority import SessionAuthority, initialize_session_authority
+ runner = GatewayRunner()
+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='off-loop')
+ authority._schedule = lambda ref: None # the admission write is the subject, not the turn
+ adapter = MSGraphWebhookAdapter(PlatformConfig(enabled=True, extra={
+ 'host': '127.0.0.1', 'client_state': 'owned', 'accepted_resources': ['users/owned']}))
+ runner.adapters[Platform.MSGRAPH_WEBHOOK] = adapter
+ runner._wire_adapter_handlers(adapter)
+ event = adapter._build_message_event({'id': 'n1', 'subscriptionId': 's', 'resource': 'users/owned/x'}, 'id:n1')
+ assert isinstance(authority, SessionAuthority)
+ return authority, lambda: adapter._admit_notification(event)
+
+
[email protected]('site', [_msgraph], ids=['msgraph_webhook._admit_notification'])
+async def test_ingress_admission_never_blocks_the_owner_loop_on_a_held_writer(tmp_path, monkeypatch, site):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ authority, admit = await site(tmp_path)
+ held, release = threading.Event(), threading.Event()
+
+ def hold_writer():
+ conn = sqlite3.connect(authority.db.db_path, timeout=30)
+ conn.execute('BEGIN IMMEDIATE')
+ held.set()
+ release.wait(30)
+ conn.rollback()
+ conn.close()
+ holder = threading.Thread(target=hold_writer, daemon=True)
+ holder.start()
+ assert await asyncio.to_thread(held.wait, 10)
+ try:
+ task = asyncio.create_task(admit())
+ ticks, start = 0, time.monotonic()
+ while time.monotonic() - start < 1.0:
+ await asyncio.sleep(0.05)
+ ticks += 1
+ # A loop parked in a blocking BEGIN IMMEDIATE runs no ticks until the writer lets go.
+ assert ticks >= 10, f'owner loop stalled behind the held writer ({ticks} ticks in 1s)'
+ assert not task.done(), 'the admission must wait for the writer, not skip the commit'
+ finally:
+ release.set()
+ receipt = await asyncio.wait_for(task, 30)
+ assert receipt.status == 'queued' # committed before the ACK, as before
+ await asyncio.to_thread(holder.join, 10)
+ await asyncio.to_thread(authority.db.close)