Skip to content

The agent that grows with you

fix(buzz): coalesce deferred cursor writes into one per adapter

Teknium110f62a7e269
diff --git a/gateway/session_ingress_burst.py b/gateway/session_ingress_burst.py
index 077faefe974..73b2614afac 100644
--- a/gateway/session_ingress_burst.py
+++ b/gateway/session_ingress_burst.py
@@ -182,18 +182,26 @@ def _undurable(adapter):
async def when_returned_inputs_durable(adapter, persist):
"""Run ``persist()`` once every input a serial reader returned on is committed: a reader that
saves its own receive cursor after the call returns (Buzz) must not advance it past a held burst,
- or a restart in the hold window loses it. Inline when nothing is pending; otherwise in a tracked
- task, so the reader reads on (a lost cursor write only replays inputs admission already holds).
- ``persist`` must carry the cursor as of this call: by the time a deferred write runs, the reader
- has already marked later inputs seen that are not committed yet."""
+ or a restart in the hold window loses it. Inline when nothing is pending; otherwise one tracked
+ task per adapter, so the reader reads on (a lost cursor write only replays inputs admission
+ already holds). ``persist`` carries the cursor as of this call; while that task waits, a newer
+ call replaces it, and the task picks the newest in the same step that saw nothing pending, so
+ an input that arrives during the write's I/O is never in it."""
+ if getattr(adapter, '_durable_persist', None) is not None:
+ adapter._durable_persist = persist
+ return
if not _undurable(adapter):
await persist()
return
+ adapter._durable_persist = persist
async def later():
- while pending := _undurable(adapter):
- await asyncio.wait(pending)
- await persist()
+ try:
+ while pending := _undurable(adapter):
+ await asyncio.wait(pending)
+ finally:
+ latest = adapter.__dict__.pop('_durable_persist', None)
+ await latest()
_track(adapter, asyncio.create_task(later()))
diff --git a/tests/gateway/test_busy_burst_serial_reader.py b/tests/gateway/test_busy_burst_serial_reader.py
index 485bb1e8171..1ac19c8214d 100644
--- a/tests/gateway/test_busy_burst_serial_reader.py
+++ b/tests/gateway/test_busy_burst_serial_reader.py
@@ -334,3 +334,25 @@ async def test_buzz_cursor_never_passes_an_uncommitted_input(monkeypatch):
assert saved[0][0] == 1, saved
# Every saved cursor names only events that had an admission row when it was written.
assert all(f'e{n}' in rows for last_ts, rows in saved for n in range(1, last_ts + 1)), saved
+
+
+async def test_buzz_cursor_write_waiting_on_disk_skips_a_newer_held_burst(monkeypatch):
+ """e1's deferred write is due but waits behind a slow earlier write; meanwhile e2 arrives and is
+ held. The waiting write must still land the cursor as it was when e1 committed, not e2's."""
+ adapter, state, source = _buzz(monkeypatch)
+ owner = await _busy_owner(monkeypatch, adapter, source, busy_text_mode='queue')
+ saved = []
+ monkeypatch.setattr(type(adapter), '_write_cursors', staticmethod(lambda path, payload: saved.append(
+ (payload['channels']['chan']['last_ts'], {row['request_id'] for row in _rows(owner.authority)}))))
+ await adapter._cursor_write_lock.acquire() # the earlier write, still in its fsync
+ await adapter._handle_events('chan', state, [_buzz_event(1, 'held')])
+ await async_wait_until(lambda: adapter._cursor_write_lock._waiters, timeout=15,
+ what="e1's committed write to queue on the lock")
+ await adapter._handle_events('chan', state, [_buzz_event(2, 'next')])
+ assert owner.key in adapter._shared_bursts # e2 is held, not committed
+ adapter._cursor_write_lock.release()
+ await async_wait_until(lambda: saved, timeout=15, what='the waiting cursor write')
+ assert saved[0][0] == 1, saved
+ assert await owner.settle(3) == [('held', 0), ('next', 0)]
+ assert all(f'e{n}' in rows for last_ts, rows in saved for n in range(1, last_ts + 1)), saved