Skip to content

Commit 12712cc

Browse files
committed
fix(matrix): prevent gateway stop on Megolm missing-key decryption failures
- Reduce retry attempts for MEGOLM_UNKNOWN_INBOUND_SESSION_ID from 8 to 2 (keys were never shared, so retrying is futile) - Add isRecoverableSyncError() to classify decryption, Megolm, and transient network errors as recoverable instead of fatal - Prevent sync.unexpected_error from treating recoverable errors as fatal Closes #93501
1 parent 102c1f4 commit 12712cc

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎extensions/matrix/src/matrix/monitor/sync-lifecycle.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@ import type { MatrixClient } from "../sdk.js";
33
import { isMatrixTerminalSyncState, type MatrixSyncState } from "../sync-state.js";
44
import type { MatrixMonitorStatusController } from "./status.js";
55

6+
/**
7+
* Determines if a sync error is recoverable (non-fatal).
8+
* Decryption failures, Megolm errors, and transient network issues should not
9+
* stop the entire sync process. These are expected during normal operation.
10+
*/
11+
function isRecoverableSyncError(error?: unknown): boolean {
12+
if (!error) {
13+
return false;
14+
}
15+
const message = error instanceof Error ? error.message : String(error);
16+
const lowerMessage = message.toLowerCase();
17+
18+
// Decryption and Megolm errors are recoverable (keys may arrive later)
19+
if (lowerMessage.includes("decrypt") || lowerMessage.includes("megolm")) {
20+
return true;
21+
}
22+
23+
// Session/key-related errors are often transient
24+
if (lowerMessage.includes("session") && lowerMessage.includes("key")) {
25+
return true;
26+
}
27+
28+
// Transient network errors
29+
if (lowerMessage.includes("network") || lowerMessage.includes("timeout") || lowerMessage.includes("offline")) {
30+
return true;
31+
}
32+
33+
// M error codes for network issues
34+
if (lowerMessage.includes("m_unknown_token") || lowerMessage.includes("m_limit_exceeded")) {
35+
return true;
36+
}
37+
38+
return false;
39+
}
40+
641
function formatSyncLifecycleError(state: MatrixSyncState, error?: unknown): Error {
742
if (error instanceof Error) {
843
return error;
@@ -38,6 +73,11 @@ export function createMatrixMonitorSyncLifecycle(params: {
3873

3974
const onSyncState = (state: MatrixSyncState, _prevState: string | null, error?: unknown) => {
4075
if (isMatrixTerminalSyncState(state) && !params.isStopping?.()) {
76+
// Recoverable errors (decryption, Megolm, network) should not stop sync
77+
if (isRecoverableSyncError(error)) {
78+
params.statusController.noteSyncState(state, error);
79+
return;
80+
}
4181
const fatalErrorLocal = formatSyncLifecycleError(state, error);
4282
params.statusController.noteUnexpectedError(fatalErrorLocal);
4383
settleFatal(fatalErrorLocal);
@@ -61,6 +101,11 @@ export function createMatrixMonitorSyncLifecycle(params: {
61101
if (params.isStopping?.()) {
62102
return;
63103
}
104+
// Recoverable errors (decryption, Megolm, network) should not be treated as fatal
105+
if (isRecoverableSyncError(error)) {
106+
params.statusController.noteSyncState("SYNCING", error);
107+
return;
108+
}
64109
params.statusController.noteUnexpectedError(error);
65110
settleFatal(error);
66111
};

‎extensions/matrix/src/matrix/sdk/decrypt-bridge.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ const MATRIX_DECRYPT_RETRY_BASE_DELAY_MS = 1_500;
3434
const MATRIX_DECRYPT_RETRY_MAX_DELAY_MS = 30_000;
3535
const MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS = 8;
3636

37+
/**
38+
* Resolves the maximum number of decryption retry attempts for a given event.
39+
* For missing-key errors (MEGOLM_UNKNOWN_INBOUND_SESSION_ID), keys were never shared,
40+
* so retrying is futile. Limit retries to 2 to avoid wasting ~2.5 minutes.
41+
*/
42+
function resolveDecryptRetryMaxAttempts(event: MatrixEvent, reason: DecryptionFailureCode | null): number {
43+
if (reason === DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID) {
44+
return 2;
45+
}
46+
return MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS;
47+
}
48+
3749
function resolveDecryptRetryKey(roomId: string, eventId: string): string | null {
3850
if (!roomId || !eventId) {
3951
return null;
@@ -271,7 +283,9 @@ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
271283
return;
272284
}
273285
const attempts = (existing?.attempts ?? 0) + 1;
274-
if (attempts > MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS) {
286+
const reason = getDecryptionFailureReason(params.event);
287+
const maxAttempts = resolveDecryptRetryMaxAttempts(params.event, reason);
288+
if (attempts > maxAttempts) {
275289
const retry = this.decryptRetries.get(retryKey);
276290
if (retry?.timer) {
277291
clearTimeout(retry.timer);

0 commit comments

Comments
 (0)