Skip to content

The agent that grows with you

fix(buzz): a deferred cursor write never names an uncommitted event

Tekniumb9d49d3d77ae
diff --git a/gateway/session_ingress_burst.py b/gateway/session_ingress_burst.py
index 2df1a94b1d9..077faefe974 100644
--- a/gateway/session_ingress_burst.py
+++ b/gateway/session_ingress_burst.py
@@ -183,7 +183,9 @@ 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)."""
+ 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."""
if not _undurable(adapter):
await persist()
return
diff --git a/plugins/platforms/buzz/adapter.py b/plugins/platforms/buzz/adapter.py
index 8a20c151e16..4dbd0aeb74b 100644
--- a/plugins/platforms/buzz/adapter.py
+++ b/plugins/platforms/buzz/adapter.py
@@ -567,6 +567,7 @@ class BuzzAdapter(BuzzCursorMixin, BasePlatformAdapter):
self._restored_cursors: dict[str, dict] = {}
# Orders off-loop cursor writes: each snapshot is taken under it, so an older one never lands last.
self._cursor_write_lock = asyncio.Lock()
+ self._cursor_snapshots = self._cursor_written = 0 # cursor write generations taken / landed
self._channel_names: dict[str, str] = {}
# channel_id -> raw ``channels list`` entry; drives DM-vs-channel classification.
self._channel_meta: dict[str, dict] = {}
@@ -1337,7 +1338,7 @@ class BuzzAdapter(BuzzCursorMixin, BasePlatformAdapter):
self._trim_seen(state)
if self._cursor_mark(state) != before: # never past a busy input held uncommitted (serial reader)
from gateway.session_ingress_burst import when_returned_inputs_durable
- await when_returned_inputs_durable(self, self._persist_cursors)
+ await when_returned_inputs_durable(self, self._cursor_writer())
@staticmethod
def _parse_imeta_attachments(event: dict) -> tuple[list[dict], int]:
diff --git a/plugins/platforms/buzz/buzz_cursors.py b/plugins/platforms/buzz/buzz_cursors.py
index d10b84d8ef6..1287fefc7dd 100644
--- a/plugins/platforms/buzz/buzz_cursors.py
+++ b/plugins/platforms/buzz/buzz_cursors.py
@@ -72,7 +72,17 @@ class BuzzCursorMixin:
seen = state.get("seen") or ()
return int(state.get("last_ts") or 0), len(seen), (next(reversed(seen), None) if seen else None)
- async def _persist_cursors(self) -> None:
- # The write fsyncs + renames, and on the WebSocket transport this runs once per inbound event.
- async with self._cursor_write_lock:
- await asyncio.to_thread(self._write_cursors, self._cursor_path(), self._cursor_payload())
+ def _cursor_writer(self):
+ """A write of the cursor as it stands now. A deferred write runs after the reader marked later
+ events seen before their dispatch committed, so it must never re-read the live state; and a
+ write older than one already landed is skipped, so the file never moves back."""
+ payload = self._cursor_payload()
+ self._cursor_snapshots = generation = self._cursor_snapshots + 1
+
+ async def persist() -> None:
+ # The write fsyncs + renames, and on the WebSocket transport this runs once per inbound event.
+ async with self._cursor_write_lock:
+ if generation > self._cursor_written:
+ self._cursor_written = generation
+ await asyncio.to_thread(self._write_cursors, self._cursor_path(), payload)
+ return persist
diff --git a/tests/gateway/test_busy_burst_serial_reader.py b/tests/gateway/test_busy_burst_serial_reader.py
index e3f09d8ee07..485bb1e8171 100644
--- a/tests/gateway/test_busy_burst_serial_reader.py
+++ b/tests/gateway/test_busy_burst_serial_reader.py
@@ -309,16 +309,28 @@ def _buzz_event(i, text):
@pytest.mark.asyncio
-async def test_buzz_cursor_never_passes_a_held_burst(monkeypatch):
- """Buzz saves its receive cursor after ``_handle_events`` returns. A held busy input is not durable
- yet, so the saved cursor must not pass it: a restart inside the hold window would skip it."""
+async def test_buzz_cursor_never_passes_an_uncommitted_input(monkeypatch):
+ """Buzz saves its receive cursor after ``_handle_events`` returns, deferred while a busy burst is
+ held. The reader marks the next event seen before dispatching it, so a write that lands in that gap
+ must still name only committed inputs: a restart restores the saved cursor and skips the rest."""
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'], holds_busy_burst(adapter, owner.key)))))
+ (payload['channels']['chan']['last_ts'], {row['request_id'] for row in _rows(owner.authority)}))))
await adapter._handle_events('chan', state, [_buzz_event(1, 'held')])
- assert owner.key in adapter._shared_bursts # returned with the input only held
- assert await owner.settle(2) == [('held', 0)]
- # The cursor naming event 1 was saved, and only once its burst had committed.
- assert [last_ts for last_ts, _ in saved] == [1] and not any(held for _, held in saved), saved
+ assert owner.key in adapter._shared_bursts and saved == [] # returned with the input only held
+ gate, real_name = asyncio.Event(), adapter._resolve_user_name
+
+ async def slow_name(pubkey): # e2 is marked seen, not yet dispatched, while the burst commits
+ await gate.wait()
+ return await real_name(pubkey)
+ adapter._resolve_user_name = slow_name
+ second = asyncio.create_task(adapter._handle_events('chan', state, [_buzz_event(2, 'next')]))
+ await async_wait_until(lambda: saved, timeout=15, what='the deferred cursor write')
+ gate.set()
+ await asyncio.wait_for(second, 10)
+ assert await owner.settle(3) == [('held', 0), ('next', 0)]
+ 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