Skip to content

Commit 8bf8f5d

Browse files
committed
fix(matrix): retain incomplete retirement poison
1 parent 77f4df4 commit 8bf8f5d

3 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎extensions/matrix/src/matrix/client/shared.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ describe("shared Matrix client generations", () => {
393393
expect(transient.abortSignal.aborted).toBe(true);
394394
expect(client.stopWithoutPersist).toHaveBeenCalledTimes(1);
395395
expect(client.stopAndPersist).not.toHaveBeenCalled();
396+
await expect(acquireSharedMatrixClient({ auth })).rejects.toMatchObject({
397+
message: "Matrix transient leases did not drain within 5000ms",
398+
});
399+
expect(createMatrixClientMock).toHaveBeenCalledTimes(1);
396400

397401
const firstLateRelease = transient.release({ mode: "persist" });
398402
const secondLateRelease = transient.release({ mode: "persist" });
@@ -604,6 +608,26 @@ describe("shared Matrix client generations", () => {
604608
await replacement.release({ mode: "discard" });
605609
});
606610

611+
it("keeps monitor cleanup failures poisoned", async () => {
612+
const cause = new Error("monitor cleanup failed");
613+
const client = createMockClient("main");
614+
createMatrixClientMock.mockResolvedValue(client);
615+
const auth = authFor("main");
616+
const monitor = await acquireSharedMatrixClient({
617+
auth,
618+
role: "monitor",
619+
startClient: false,
620+
});
621+
const retirement = createMonitorRetirement([]);
622+
retirement.cleanup.mockRejectedValue(cause);
623+
624+
monitor.registerMonitorRetirement(retirement);
625+
await expect(monitor.release({ mode: "persist" })).rejects.toBe(cause);
626+
expect(client.stopWithoutPersist).toHaveBeenCalledTimes(1);
627+
await expect(acquireSharedMatrixClient({ auth })).rejects.toBe(cause);
628+
expect(createMatrixClientMock).toHaveBeenCalledTimes(1);
629+
});
630+
607631
it("preserves an earlier stop requirement when the final lease requests discard", async () => {
608632
const cause = new Error("best-effort persistence failed");
609633
const persist = createDeferred<void>();

‎extensions/matrix/src/matrix/client/shared.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,35 +345,43 @@ function beginGenerationRetirement(params: {
345345
}
346346
state.phase = "quiescing";
347347
state.retirementPromise = Promise.resolve().then(async () => {
348+
let canReplacePoisonedGeneration = false;
348349
try {
349350
await state.client.quiesceSync();
350351
state.started = false;
351352
await state.client.drainPendingDecryptions("matrix monitor sync quiesce");
352353
} catch (error) {
353354
state.poisonError = toRetirementError(error);
355+
canReplacePoisonedGeneration = true;
354356
}
355357

356358
try {
357359
await retireMonitorLeases(state, params.monitorLeases ?? []);
358360
} catch (error) {
359361
state.poisonError ??= toRetirementError(error);
362+
canReplacePoisonedGeneration = false;
360363
}
361364

362365
state.phase = "closing";
363366
try {
364367
await waitForLeaseDrain(state);
365368
} catch (error) {
366369
state.poisonError ??= toRetirementError(error);
370+
canReplacePoisonedGeneration = false;
367371
forceReleaseLeases(state);
368372
}
369373

370374
if (state.poisonError) {
371-
try {
372-
await state.client
373-
.drainPendingDecryptions("matrix poisoned client shutdown")
374-
.catch(() => undefined);
375-
state.client.stopWithoutPersist();
376-
} finally {
375+
const decryptionsDrained = await state.client
376+
.drainPendingDecryptions("matrix poisoned client shutdown")
377+
.then(
378+
() => true,
379+
() => false,
380+
);
381+
state.client.stopWithoutPersist();
382+
// Only sync/decryption poison is replaceable, after every other owner retired
383+
// and the discarded client conclusively drained and stopped.
384+
if (canReplacePoisonedGeneration && decryptionsDrained) {
377385
deleteSharedClientState(state);
378386
}
379387
throw state.poisonError;

‎extensions/matrix/src/matrix/sdk.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,8 +1651,13 @@ describe("MatrixClient request hardening", () => {
16511651
);
16521652
await vi.advanceTimersByTimeAsync(5_000);
16531653
await rejection;
1654-
await expect(client.quiesceSync()).rejects.toThrow(
1655-
"Matrix classic sync did not reach STOPPED within 5000ms",
1654+
const repeatedOutcome = client.quiesceSync().then(
1655+
() => "resolved",
1656+
() => "rejected",
1657+
);
1658+
await vi.advanceTimersByTimeAsync(0);
1659+
await expect(Promise.race([repeatedOutcome, Promise.resolve("pending")])).resolves.toBe(
1660+
"rejected",
16561661
);
16571662

16581663
expect(syncInternals.connectionReturnedResolvers).toBeUndefined();

0 commit comments

Comments
 (0)