Skip to content

The agent that grows with you

fix: rotate authority replay epoch on concurrent eviction

Tekniuma9de35d29ae8
diff --git a/gateway/session_events.py b/gateway/session_events.py
index 245e23a225b..d8e06a02448 100644
--- a/gateway/session_events.py
+++ b/gateway/session_events.py
@@ -33,6 +33,10 @@ class SessionEvents:
'type': 'message.complete', 'session_id': self._key,
'payload': deepcopy(payload), 'replay_epoch': self.epoch}}
event_replay._stamp_event(frame)
+ if frame['params']['seq'] <= self.sequence:
+ # Another publisher may evict us after watermark's lookup.
+ self.epoch = uuid.uuid4().hex
+ frame['params']['replay_epoch'] = self.epoch
# The ring owns this same event object; its lookup key remains
# private while both replay and live recipients see canonical IDs.
frame['params']['session_id'] = session_id
diff --git a/tests/gateway/test_authority_events.py b/tests/gateway/test_authority_events.py
index a6507cace6b..af3c150c441 100644
--- a/tests/gateway/test_authority_events.py
+++ b/tests/gateway/test_authority_events.py
@@ -98,6 +98,17 @@ async def test_replay_matches_subscription_watermark_or_requires_snapshot(tmp_pa
after = (await since(refreshed['replay_epoch'], refreshed['last_sequence']))['result']
assert not after['snapshot_required'] and len(after['events']) == 1
assert after['events'][0]['payload']['text'] == '5'
+ before_race = await b.resume(ref, {})
+ stamp = event_replay._stamp_event
+ def evict_then_stamp(frame):
+ stamp({'method': 'event', 'params': {'session_id': 'competing-owner'}})
+ stamp(frame)
+ with monkeypatch.context() as race:
+ race.setattr(event_replay, '_stamp_event', evict_then_stamp)
+ await turn(6)
+ raced = (await since(before_race['replay_epoch'], before_race['last_sequence']))['result']
+ assert raced['snapshot_required'], 'eviction during stamp reused an old epoch/sequence'
+ assert raced['replay_epoch'] != before_race['replay_epoch']
finally:
await a.close()
await b.close()