Skip to content

Commit 39a4a19

Browse files
fix(matrix): queue later admissions behind a still-parked predecessor
1 parent 92da25f commit 39a4a19

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎extensions/matrix/src/matrix/monitor/ingress.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,50 @@ describe("Matrix durable ingress", () => {
169169
});
170170
});
171171

172+
it("queues a later same-room event behind a still-failing parked predecessor", async () => {
173+
await withQueue(async (queue) => {
174+
const dispatched: string[] = [];
175+
const dispatch = vi.fn(async (_roomId: string, event: MatrixRawEvent) => {
176+
dispatched.push(event.event_id ?? "");
177+
});
178+
const monitor = createMonitor({ queue, dispatch, pollIntervalMs: 10 });
179+
try {
180+
// Keep every journal append failing until the test flips the flag:
181+
// armed parked retries fire between accepts, so a fixed rejection
182+
// count would let a retry recover the queue mid-assertion.
183+
let queueHealthy = false;
184+
const originalEnqueue = queue.enqueue.bind(queue);
185+
const enqueueSpy = vi.spyOn(queue, "enqueue").mockImplementation((...args) => {
186+
if (!queueHealthy) {
187+
return Promise.reject(new Error("sqlite busy"));
188+
}
189+
return originalEnqueue(...args);
190+
});
191+
await monitor.accept("!room:example.org", createRawEvent("$evt-older"));
192+
expect(monitor.getAdmissionFailure()).toBeInstanceOf(Error);
193+
194+
// The newer event must not reach the journal while the predecessor
195+
// is still parked: every attempt so far belongs to the older event.
196+
await monitor.accept("!room:example.org", createRawEvent("$evt-newer"));
197+
expect(enqueueSpy.mock.calls.map((call) => call[0])).not.toContain("$evt-newer");
198+
expect(enqueueSpy.mock.calls.length).toBeGreaterThan(0);
199+
expect(await queue.listPending({ limit: 10 })).toEqual([]);
200+
expect(monitor.getAdmissionFailure()).toBeInstanceOf(Error);
201+
202+
// Recovery keeps accept order: the poll retry lands both parked
203+
// events and the drain dispatches older before newer.
204+
queueHealthy = true;
205+
monitor.start();
206+
await vi.waitFor(() => {
207+
expect(dispatched).toEqual(["$evt-older", "$evt-newer"]);
208+
});
209+
expect(monitor.getAdmissionFailure()).toBe(null);
210+
} finally {
211+
await monitor.stop();
212+
}
213+
});
214+
});
215+
172216
it("commits the journal row before the sync token persist can fire", async () => {
173217
await withQueue(async (queue) => {
174218
const dispatch = vi.fn(async () => {});

‎extensions/matrix/src/matrix/monitor/ingress.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,9 @@ export function createMatrixIngressMonitor(options: {
322322
// retried on later drain polls until it lands. While any admission stays
323323
// parked the sync-token store refuses to advance the durable cursor past
324324
// it, so the event is never skipped — and the cursor unfreezes as soon as
325-
// the journal append succeeds again instead of needing a restart.
325+
// the journal append succeeds again instead of needing a restart. Later
326+
// accepts queue behind the parked tail without touching the journal, so
327+
// queue rows always land in accept order.
326328
const parkedAdmissions: Array<{
327329
roomId: string;
328330
event: MatrixRawEvent;
@@ -399,6 +401,16 @@ export function createMatrixIngressMonitor(options: {
399401
// retries rather than dispatching live around the drain's dedupe and
400402
// lane serialization.
401403
const admission = { roomId, event, facts, receivedAt: Date.now() };
404+
// A still-parked predecessor must enter the journal first. Appending
405+
// this newer event now could succeed while the older one is parked, and
406+
// the drain would dispatch the room out of accept order — the queue can
407+
// only order persisted rows. Queue behind the parked tail; the
408+
// poll-driven retry lands both in order.
409+
if (parkedAdmissions.length > 0) {
410+
parkedAdmissions.push(admission);
411+
scheduleAdmissionRetry();
412+
return;
413+
}
402414
let lastError: unknown;
403415
for (const delayMs of [0, 100, 300]) {
404416
if (delayMs > 0) {
@@ -424,8 +436,9 @@ export function createMatrixIngressMonitor(options: {
424436

425437
return {
426438
accept: (roomId, event) => {
427-
// Parked retries run first so a recovered queue cannot append a later
428-
// event ahead of an earlier parked one.
439+
// Parked retries run first, and admitOnce itself queues behind any
440+
// still-parked predecessor, so journal order always matches accept
441+
// order even while the queue keeps failing.
429442
const admission = admissionTail
430443
.then(() => retryParkedAdmissions())
431444
.then(() => admitOnce(roomId, event));

0 commit comments

Comments
 (0)