fix(matrix): handle event decryption errors during sync (#94416)

Co-authored-by: mushuiyu886 <mushuiyu886@users.noreply.github.com>
This commit is contained in:
mushuiyu886
2026-07-13 00:32:05 -07:00
committed by GitHub
co-authored by mushuiyu886
parent 73da94f06f
commit 4200745a98
3 changed files with 510 additions and 31 deletions
@@ -74,6 +74,29 @@ describe("createMatrixMonitorSyncLifecycle", () => {
});
});
it("treats missing-key-shaped unexpected sync errors as fatal", async () => {
const { client, lifecycle, setStatus } = createSyncLifecycleHarness();
const waitPromise = lifecycle.waitForFatalStop();
const error = new Error(
"Error decrypting event (id=$event type=m.room.encrypted): DecryptionError[msg: The sender's device has not sent us the keys for this message.]",
);
try {
client.emit("sync.unexpected_error", error);
await Promise.resolve();
expectLastStatusFields(setStatus, {
accountId: "default",
healthState: "error",
lastError: error.message,
});
await expect(waitPromise).rejects.toThrow(error.message);
} finally {
lifecycle.dispose();
await waitPromise.catch(() => undefined);
}
});
it("ignores STOPPED emitted during intentional shutdown", async () => {
const { client, lifecycle, setStatus, setStopping } = createSyncLifecycleHarness({
withStopping: true,
+388 -20
View File
@@ -4,10 +4,12 @@ import { EventEmitter } from "node:events";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { CryptoEvent } from "matrix-js-sdk/lib/crypto-api/CryptoEvent.js";
import { resetPluginStateStoreForTests } from "openclaw/plugin-sdk/plugin-state-test-runtime";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { installMatrixTestRuntime } from "../test-runtime.js";
import { readMatrixRecoveryKeyState } from "./crypto-state-store.js";
import { MatrixDecryptBridge } from "./sdk/decrypt-bridge.js";
function requestUrl(input: RequestInfo | URL | undefined): string {
if (!input) {
@@ -96,16 +98,24 @@ class FakeMatrixEvent extends EventEmitter {
private readonly roomId: string;
private readonly eventId: string;
private readonly sender: string;
private readonly encrypted: boolean;
private type: string;
private readonly ts: number;
private content: Record<string, unknown>;
private clearEvent?: { type: string; content: Record<string, unknown> };
private readonly stateKey?: string;
private readonly unsigned?: {
age?: number;
redacted_because?: unknown;
};
readonly decryptionFailureReason?: string;
private decryptionFailureReasonValue?: string;
private decryptionFailure: boolean;
private decryptAttemptHandler?: (options?: { isRetry?: boolean }) => Promise<void> | void;
readonly attemptDecryption = vi.fn(
async (_crypto: unknown, options?: { isRetry?: boolean }): Promise<void> => {
await this.decryptAttemptHandler?.(options);
},
);
constructor(params: {
roomId: string;
@@ -126,15 +136,20 @@ class FakeMatrixEvent extends EventEmitter {
this.roomId = params.roomId;
this.eventId = params.eventId;
this.sender = params.sender;
this.encrypted = params.type === "m.room.encrypted";
this.type = params.type;
this.ts = params.ts;
this.content = params.content;
this.stateKey = params.stateKey;
this.unsigned = params.unsigned;
this.decryptionFailureReason = params.decryptionFailureReason;
this.decryptionFailureReasonValue = params.decryptionFailureReason;
this.decryptionFailure = params.decryptionFailure === true;
}
get decryptionFailureReason(): string | undefined {
return this.decryptionFailureReasonValue;
}
getRoomId(): string {
return this.roomId;
}
@@ -148,7 +163,7 @@ class FakeMatrixEvent extends EventEmitter {
}
getType(): string {
return this.type;
return this.clearEvent?.type ?? this.type;
}
getTs(): number {
@@ -156,7 +171,7 @@ class FakeMatrixEvent extends EventEmitter {
}
getContent(): Record<string, unknown> {
return this.content;
return this.clearEvent?.content ?? this.content;
}
getUnsigned(): { age?: number; redacted_because?: unknown } {
@@ -171,10 +186,33 @@ class FakeMatrixEvent extends EventEmitter {
return this.decryptionFailure;
}
shouldAttemptDecryption(): boolean {
return this.encrypted && this.clearEvent === undefined;
}
onAttemptDecryption(handler: (options?: { isRetry?: boolean }) => Promise<void> | void): void {
this.decryptAttemptHandler = handler;
}
markDecryptionFailed(reason: string): void {
this.clearEvent = {
type: "m.room.message",
content: {
msgtype: "m.bad.encrypted",
body: `** Unable to decrypt: ${reason} **`,
},
};
this.decryptionFailure = true;
this.decryptionFailureReasonValue ??= DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID;
this.emit("decrypted", this, new Error(reason));
}
markDecrypted(params: { type: string; content: Record<string, unknown> }): void {
this.type = params.type;
this.content = params.content;
this.clearEvent = { type: params.type, content: params.content };
this.decryptionFailure = false;
this.decryptionFailureReasonValue = undefined;
}
}
@@ -207,7 +245,7 @@ type MatrixJsClientStub = {
sendTyping: ReturnType<typeof vi.fn>;
getRoom: ReturnType<typeof vi.fn>;
getRooms: ReturnType<typeof vi.fn>;
getCrypto: ReturnType<typeof vi.fn>;
getCrypto: ReturnType<typeof vi.fn<() => unknown>>;
decryptEventIfNeeded: ReturnType<typeof vi.fn>;
relations: ReturnType<typeof vi.fn>;
};
@@ -989,7 +1027,7 @@ describe("MatrixClient event bridge", () => {
body: "hello",
},
});
matrixJsClient.decryptEventIfNeeded = vi.fn(async () => {
encrypted.onAttemptDecryption(() => {
encrypted.emit("decrypted", decrypted);
});
@@ -1004,8 +1042,9 @@ describe("MatrixClient event bridge", () => {
expect(trigger).toBeTypeOf("function");
trigger?.();
await Promise.resolve();
await Promise.resolve();
expect(matrixJsClient.decryptEventIfNeeded).toHaveBeenCalledTimes(1);
expect(encrypted.attemptDecryption).toHaveBeenCalledTimes(1);
expect(delivered).toEqual(["m.room.message"]);
});
@@ -1170,10 +1209,145 @@ describe("MatrixClient event bridge", () => {
expect(failed).toEqual(["missing room key"]);
});
it("does not start duplicate retries when crypto signals fire while retry is in-flight", async () => {
it("retries an exhausted missing-key decryption when a crypto key signal arrives", async () => {
vi.useFakeTimers();
const cryptoStateDir = fs.mkdtempSync(path.join(os.tmpdir(), "matrix-sdk-exhausted-retry-"));
const client = new MatrixClient("https://matrix.example.org", "token", {
encryption: true,
idbSnapshotPath: path.join(cryptoStateDir, "crypto-idb-snapshot.json"),
cryptoDatabasePrefix: path.basename(cryptoStateDir),
});
const delivered: string[] = [];
const cryptoListeners = new Map<string, (...args: unknown[]) => void>();
matrixJsClient.getCrypto = vi.fn(() => ({
on: vi.fn((eventName: string, listener: (...args: unknown[]) => void) => {
cryptoListeners.set(eventName, listener);
}),
bootstrapCrossSigning: vi.fn(async () => {}),
bootstrapSecretStorage: vi.fn(consumeMatrixSecretStorageKey),
requestOwnUserVerification: vi.fn(async () => null),
}));
client.on("room.message", (_roomId, event) => {
delivered.push(event.type);
});
const encrypted = new FakeMatrixEvent({
roomId: "!room:example.org",
eventId: "$event",
sender: "@alice:example.org",
type: "m.room.encrypted",
ts: Date.now(),
content: {},
decryptionFailure: true,
});
matrixJsClient.decryptEventIfNeeded = vi.fn(
async (event: FakeMatrixEvent, options?: { isRetry?: boolean }) => {
if (!event.shouldAttemptDecryption()) {
return;
}
await event.attemptDecryption(matrixJsClient.getCrypto(), options);
},
);
await client.start();
try {
matrixJsClient.emit("event", encrypted);
encrypted.markDecryptionFailed("missing room key");
await vi.advanceTimersByTimeAsync(200_000);
expect(encrypted.attemptDecryption).toHaveBeenCalledTimes(8);
encrypted.attemptDecryption.mockClear();
encrypted.onAttemptDecryption(() => {
encrypted.markDecrypted({
type: "m.room.message",
content: {
msgtype: "m.text",
body: "hello",
},
});
});
const trigger = cryptoListeners.get("crypto.keyBackupDecryptionKeyCached");
expect(trigger).toBeTypeOf("function");
trigger?.();
await Promise.resolve();
await Promise.resolve();
expect(encrypted.attemptDecryption).toHaveBeenCalledTimes(1);
expect(delivered).toEqual(["m.room.message"]);
} finally {
client.stopSyncWithoutPersist();
}
});
it("clears exhausted decrypt retries when the bridge stops", async () => {
vi.useFakeTimers();
const cryptoStateDir = fs.mkdtempSync(
path.join(os.tmpdir(), "matrix-sdk-stop-exhausted-retry-"),
);
const client = new MatrixClient("https://matrix.example.org", "token", {
encryption: true,
idbSnapshotPath: path.join(cryptoStateDir, "crypto-idb-snapshot.json"),
cryptoDatabasePrefix: path.basename(cryptoStateDir),
});
const cryptoListeners = new Map<string, (...args: unknown[]) => void>();
matrixJsClient.getCrypto = vi.fn(() => ({
on: vi.fn((eventName: string, listener: (...args: unknown[]) => void) => {
cryptoListeners.set(eventName, listener);
}),
bootstrapCrossSigning: vi.fn(async () => {}),
bootstrapSecretStorage: vi.fn(consumeMatrixSecretStorageKey),
requestOwnUserVerification: vi.fn(async () => null),
}));
const encrypted = new FakeMatrixEvent({
roomId: "!room:example.org",
eventId: "$event",
sender: "@alice:example.org",
type: "m.room.encrypted",
ts: Date.now(),
content: {},
decryptionFailure: true,
});
await client.start();
matrixJsClient.emit("event", encrypted);
encrypted.emit("decrypted", encrypted, new Error("missing room key"));
await vi.advanceTimersByTimeAsync(200_000);
expect(encrypted.attemptDecryption).toHaveBeenCalledTimes(8);
client.stopWithoutPersist();
encrypted.attemptDecryption.mockClear();
encrypted.onAttemptDecryption(() => {
encrypted.markDecrypted({
type: "m.room.message",
content: {
msgtype: "m.text",
body: "hello",
},
});
});
const trigger = cryptoListeners.get("crypto.keyBackupDecryptionKeyCached");
expect(trigger).toBeTypeOf("function");
trigger?.();
await Promise.resolve();
expect(encrypted.attemptDecryption).not.toHaveBeenCalled();
});
it("does not start duplicate retries when crypto signals fire while retry is in-flight", async () => {
const cryptoStateDir = fs.mkdtempSync(path.join(os.tmpdir(), "matrix-sdk-duplicate-retry-"));
const client = new MatrixClient("https://matrix.example.org", "token", {
encryption: true,
idbSnapshotPath: path.join(cryptoStateDir, "crypto-idb-snapshot.json"),
cryptoDatabasePrefix: path.basename(cryptoStateDir),
});
const delivered: string[] = [];
const cryptoListeners = new Map<string, (...args: unknown[]) => void>();
@@ -1213,7 +1387,7 @@ describe("MatrixClient event bridge", () => {
});
const releaseRetryRef: { current?: () => void } = {};
matrixJsClient.decryptEventIfNeeded = vi.fn(
encrypted.onAttemptDecryption(
async () =>
await new Promise<void>((resolve) => {
releaseRetryRef.current = () => {
@@ -1224,19 +1398,24 @@ describe("MatrixClient event bridge", () => {
);
await client.start();
matrixJsClient.emit("event", encrypted);
encrypted.emit("decrypted", encrypted, new Error("missing room key"));
try {
matrixJsClient.emit("event", encrypted);
encrypted.emit("decrypted", encrypted, new Error("missing room key"));
const trigger = cryptoListeners.get("crypto.keyBackupDecryptionKeyCached");
expect(trigger).toBeTypeOf("function");
trigger?.();
trigger?.();
await Promise.resolve();
const trigger = cryptoListeners.get("crypto.keyBackupDecryptionKeyCached");
expect(trigger).toBeTypeOf("function");
trigger?.();
trigger?.();
await Promise.resolve();
expect(matrixJsClient.decryptEventIfNeeded).toHaveBeenCalledTimes(1);
releaseRetryRef.current?.();
await Promise.resolve();
expect(delivered).toEqual(["m.room.message"]);
expect(encrypted.attemptDecryption).toHaveBeenCalledTimes(1);
releaseRetryRef.current?.();
await Promise.resolve();
await Promise.resolve();
expect(delivered).toEqual(["m.room.message"]);
} finally {
client.stopSyncWithoutPersist();
}
});
it("emits room.invite when a membership invite targets the current user", async () => {
@@ -3586,4 +3765,193 @@ describe("MatrixClient crypto bootstrapping", () => {
expect(result.success).toBe(true);
expect(result.error).toBeUndefined();
});
it("bounds exhausted decrypt retry rehydration on crypto signals", async () => {
const cryptoApi = {};
const bridge = new MatrixDecryptBridge({
client: {
getCrypto: () => cryptoApi,
},
toRaw: (event) => ({
event_id: event.getId() ?? "",
}),
emitDecryptedEvent: vi.fn(),
emitFailedDecryption: vi.fn(),
emitMessage: vi.fn(),
});
const retryStates = (
bridge as unknown as {
exhaustedDecryptRetries: Map<
string,
{
event: FakeMatrixEvent;
roomId: string;
eventId: string;
attempts: number;
inFlight: boolean;
timer: ReturnType<typeof setTimeout> | null;
exhaustedAt: number;
}
>;
}
).exhaustedDecryptRetries;
const events = Array.from({ length: 513 }, (_unused, index) => {
const event = new FakeMatrixEvent({
roomId: "!room:example.org",
eventId: `$event-${index}`,
sender: "@alice:example.org",
type: "m.room.encrypted",
ts: Date.now(),
content: {},
decryptionFailure: true,
});
event.onAttemptDecryption(() => {});
retryStates.set(`!room:example.org|$event-${index}`, {
event,
roomId: "!room:example.org",
eventId: `$event-${index}`,
attempts: 8,
inFlight: false,
timer: null,
exhaustedAt: Date.now(),
});
return event;
});
bridge.retryPendingNow("test crypto signal", { includeExhausted: true });
await Promise.resolve();
await Promise.resolve();
expect(events[0]?.attemptDecryption).not.toHaveBeenCalled();
expect(events.at(-1)?.attemptDecryption).toHaveBeenCalledTimes(1);
});
it("does not rehydrate exhausted decrypt retries on generic device updates", async () => {
const listeners = new Map<string, () => void>();
const cryptoApi = {
on: vi.fn((eventName: string, listener: () => void) => {
listeners.set(eventName, listener);
}),
};
const bridge = new MatrixDecryptBridge({
client: {
getCrypto: () => cryptoApi,
},
toRaw: (event) => ({
event_id: event.getId() ?? "",
}),
emitDecryptedEvent: vi.fn(),
emitFailedDecryption: vi.fn(),
emitMessage: vi.fn(),
});
const event = new FakeMatrixEvent({
roomId: "!room:example.org",
eventId: "$event",
sender: "@alice:example.org",
type: "m.room.encrypted",
ts: Date.now(),
content: {},
decryptionFailure: true,
});
event.onAttemptDecryption(() => {});
(
bridge as unknown as {
exhaustedDecryptRetries: Map<
string,
{
event: FakeMatrixEvent;
roomId: string;
eventId: string;
attempts: number;
inFlight: boolean;
timer: ReturnType<typeof setTimeout> | null;
exhaustedAt: number;
}
>;
}
).exhaustedDecryptRetries.set("!room:example.org|$event", {
event,
roomId: "!room:example.org",
eventId: "$event",
attempts: 8,
inFlight: false,
timer: null,
exhaustedAt: Date.now(),
});
bridge.bindCryptoRetrySignals(cryptoApi);
listeners.get(CryptoEvent.DevicesUpdated)?.();
await Promise.resolve();
expect(event.attemptDecryption).not.toHaveBeenCalled();
listeners.get(CryptoEvent.KeyBackupDecryptionKeyCached)?.();
await Promise.resolve();
await Promise.resolve();
expect(event.attemptDecryption).toHaveBeenCalledTimes(1);
});
it("does not rearm or emit after stop while a decrypt retry is in flight", async () => {
let releaseDecrypt: (() => void) | undefined;
const emitFailedDecryption = vi.fn();
const emitMessage = vi.fn();
const bridge = new MatrixDecryptBridge({
client: {
getCrypto: () => ({}),
},
toRaw: (event) => ({
event_id: event.getId() ?? "",
}),
emitDecryptedEvent: vi.fn(),
emitFailedDecryption,
emitMessage,
});
const event = new FakeMatrixEvent({
roomId: "!room:example.org",
eventId: "$event",
sender: "@alice:example.org",
type: "m.room.encrypted",
ts: Date.now(),
content: {},
decryptionFailure: true,
});
event.onAttemptDecryption(
() =>
new Promise<void>((resolve) => {
releaseDecrypt = () => {
event.emit("decrypted", event, new Error("still missing"));
resolve();
};
}),
);
const retryState = {
event,
roomId: "!room:example.org",
eventId: "$event",
attempts: 1,
inFlight: false,
timer: null,
};
(
bridge as unknown as {
decryptRetries: Map<string, typeof retryState>;
}
).decryptRetries.set("!room:example.org|$event", retryState);
bridge.retryPendingNow("test retry");
await Promise.resolve();
bridge.stop();
releaseDecrypt?.();
await Promise.resolve();
await Promise.resolve();
expect(emitFailedDecryption).not.toHaveBeenCalled();
expect(emitMessage).not.toHaveBeenCalled();
expect(
(
bridge as unknown as {
decryptRetries: Map<string, typeof retryState>;
}
).decryptRetries.size,
).toBe(0);
});
});
@@ -11,6 +11,16 @@ type MatrixDecryptIfNeededClient = {
isRetry?: boolean;
},
) => Promise<void>;
getCrypto?: () => unknown;
};
type MatrixDecryptRetryEvent = MatrixEvent & {
attemptDecryption?: (
crypto: unknown,
opts?: {
isRetry?: boolean;
},
) => Promise<void>;
};
type MatrixDecryptRetryState = {
@@ -22,6 +32,10 @@ type MatrixDecryptRetryState = {
timer: ReturnType<typeof setTimeout> | null;
};
type MatrixExhaustedDecryptRetryState = MatrixDecryptRetryState & {
exhaustedAt: number;
};
type DecryptBridgeRawEvent = {
event_id: string;
};
@@ -33,6 +47,8 @@ type MatrixCryptoRetrySignalSource = {
const MATRIX_DECRYPT_RETRY_BASE_DELAY_MS = 1_500;
const MATRIX_DECRYPT_RETRY_MAX_DELAY_MS = 30_000;
const MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS = 8;
const MATRIX_DECRYPT_EXHAUSTED_RETRY_TTL_MS = 60 * 60_000;
const MATRIX_DECRYPT_EXHAUSTED_RETRY_MAX_ENTRIES = 512;
function resolveDecryptRetryKey(roomId: string, eventId: string): string | null {
if (!roomId || !eventId) {
@@ -75,10 +91,11 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
private readonly decryptedMessageDedupe = new Map<string, number>();
private readonly decryptRetries = new Map<string, MatrixDecryptRetryState>();
private readonly failedDecryptionsNotified = new Set<string>();
private readonly exhaustedDecryptRetries = new Set<string>();
private readonly exhaustedDecryptRetries = new Map<string, MatrixExhaustedDecryptRetryState>();
private activeRetryRuns = 0;
private readonly retryIdleResolvers = new Set<() => void>();
private cryptoRetrySignalsBound = false;
private stopped = false;
constructor(
private readonly deps: {
@@ -104,6 +121,9 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
attachEncryptedEvent(event: MatrixEvent, roomId: string): void {
if (this.stopped) {
return;
}
if (this.trackedEncryptedEvents.has(event)) {
return;
}
@@ -123,7 +143,26 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
}
retryPendingNow(reason: string): void {
retryPendingNow(reason: string, options?: { includeExhausted?: boolean }): void {
if (this.stopped) {
return;
}
if (options?.includeExhausted) {
this.pruneExhaustedDecryptRetries(Date.now());
for (const [retryKey, state] of this.exhaustedDecryptRetries) {
if (this.decryptRetries.has(retryKey)) {
continue;
}
this.exhaustedDecryptRetries.delete(retryKey);
this.decryptRetries.set(retryKey, {
...state,
attempts: 0,
inFlight: false,
timer: null,
});
}
}
const pending = Array.from(this.decryptRetries.entries());
if (pending.length === 0) {
return;
@@ -147,15 +186,15 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
this.cryptoRetrySignalsBound = true;
const trigger = (reason: string): void => {
this.retryPendingNow(reason);
const trigger = (reason: string, options?: { includeExhausted?: boolean }): void => {
this.retryPendingNow(reason, options);
};
crypto.on(CryptoEvent.KeyBackupDecryptionKeyCached, () => {
trigger("crypto.keyBackupDecryptionKeyCached");
trigger("crypto.keyBackupDecryptionKeyCached", { includeExhausted: true });
});
crypto.on(CryptoEvent.RehydrationCompleted, () => {
trigger("dehydration.RehydrationCompleted");
trigger("dehydration.RehydrationCompleted", { includeExhausted: true });
});
crypto.on(CryptoEvent.DevicesUpdated, () => {
trigger("crypto.devicesUpdated");
@@ -166,9 +205,11 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
stop(): void {
this.stopped = true;
for (const retryKey of this.decryptRetries.keys()) {
this.clearDecryptRetry(retryKey);
}
this.exhaustedDecryptRetries.clear();
}
async drainPendingDecryptions(reason: string): Promise<void> {
@@ -193,6 +234,9 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
decryptedEvent: MatrixEvent;
err?: Error;
}): void {
if (this.stopped) {
return;
}
const decryptedRoomId = params.decryptedEvent.getRoomId() || params.roomId;
const decryptedRaw = this.deps.toRaw(params.decryptedEvent);
const retryEventId = decryptedRaw.event_id || params.encryptedEvent.getId() || "";
@@ -259,6 +303,9 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
roomId: string;
eventId: string;
}): void {
if (this.stopped) {
return;
}
const retryKey = resolveDecryptRetryKey(params.roomId, params.eventId);
if (!retryKey) {
return;
@@ -277,7 +324,17 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
clearTimeout(retry.timer);
}
this.decryptRetries.delete(retryKey);
this.exhaustedDecryptRetries.add(retryKey);
const exhaustedAt = Date.now();
this.exhaustedDecryptRetries.set(retryKey, {
event: params.event,
roomId: params.roomId,
eventId: params.eventId,
attempts: attempts - 1,
inFlight: false,
timer: null,
exhaustedAt,
});
this.pruneExhaustedDecryptRetries(exhaustedAt);
LogService.debug(
"MatrixClientLite",
`Giving up decryption retry for ${params.eventId} in ${params.roomId} after ${attempts - 1} attempts`,
@@ -311,7 +368,14 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
state.inFlight = true;
state.timer = null;
this.activeRetryRuns += 1;
const canDecrypt = typeof this.deps.client.decryptEventIfNeeded === "function";
const retryEvent = state.event as MatrixDecryptRetryEvent;
const retryCrypto = this.deps.client.getCrypto?.();
const canAttemptDecryption =
retryCrypto !== undefined &&
retryCrypto !== null &&
typeof retryEvent.attemptDecryption === "function";
const canDecrypt =
canAttemptDecryption || typeof this.deps.client.decryptEventIfNeeded === "function";
if (!canDecrypt) {
this.clearDecryptRetry(retryKey);
this.activeRetryRuns = Math.max(0, this.activeRetryRuns - 1);
@@ -320,9 +384,15 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
}
try {
await this.deps.client.decryptEventIfNeeded?.(state.event, {
isRetry: true,
});
if (canAttemptDecryption) {
await retryEvent.attemptDecryption?.(retryCrypto, {
isRetry: true,
});
} else {
await this.deps.client.decryptEventIfNeeded?.(state.event, {
isRetry: true,
});
}
} catch {
// Retry with backoff until we hit the configured retry cap.
} finally {
@@ -334,6 +404,9 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
if (this.decryptRetries.get(retryKey) !== state) {
return;
}
if (this.stopped) {
return;
}
if (isDecryptionFailure(state.event)) {
if (!shouldRetryDecryptionFailure(state.event)) {
this.clearDecryptRetry(retryKey);
@@ -360,6 +433,21 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
this.failedDecryptionsNotified.delete(retryKey);
}
private pruneExhaustedDecryptRetries(now: number): void {
for (const [retryKey, state] of this.exhaustedDecryptRetries) {
if (now - state.exhaustedAt > MATRIX_DECRYPT_EXHAUSTED_RETRY_TTL_MS) {
this.exhaustedDecryptRetries.delete(retryKey);
}
}
while (this.exhaustedDecryptRetries.size > MATRIX_DECRYPT_EXHAUSTED_RETRY_MAX_ENTRIES) {
const oldest = this.exhaustedDecryptRetries.keys().next().value;
if (oldest === undefined) {
break;
}
this.exhaustedDecryptRetries.delete(oldest);
}
}
private rememberDecryptedMessage(roomId: string, eventId: string): void {
if (!eventId) {
return;