From b9d315d2fe100ed05ed67f81ded88ec3fdbb0a9f Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Mon, 24 Aug 2026 12:50:15 +0200 Subject: [PATCH] fix(crypto): re-check key backup after the first sync --- src/client/initMatrix.test.ts | 63 ++++++++++++++++++++++++++++++++++- src/client/initMatrix.ts | 22 ++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/client/initMatrix.test.ts b/src/client/initMatrix.test.ts index af556b20f..5ed49ef60 100644 --- a/src/client/initMatrix.test.ts +++ b/src/client/initMatrix.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; -import type { MatrixClient } from '$types/matrix-sdk'; +import type { ClientEvent, MatrixClient } from '$types/matrix-sdk'; +import { SyncState } from '$types/matrix-sdk'; import type { Session } from '$state/sessions'; import type * as PlatformModule from '$utils/platform'; import { ACTIVE_SESSION_KEY, MATRIX_SESSIONS_KEY } from '$state/sessions'; @@ -17,6 +18,7 @@ import { installSlidingSyncRequestPatch, newSlidingSyncConnId, ownsActiveMediaSession, + recheckKeyBackupAfterInitialSync, resolvePollTimeoutMs, supportsSlidingSync, } from './initMatrix'; @@ -279,3 +281,62 @@ describe('resolvePollTimeoutMs', () => { expect(resolvePollTimeoutMs(5000)).toBe(5000); }); }); + +const makeKeyBackupMx = (syncState: SyncState | null) => { + const checkKeyBackupAndEnable = vi.fn<() => Promise>().mockResolvedValue(null); + const listeners = new Set<(state: SyncState) => void>(); + const mx = { + getSyncState: () => syncState, + getCrypto: () => ({ checkKeyBackupAndEnable }), + on: (_event: ClientEvent, cb: (state: SyncState) => void) => listeners.add(cb), + removeListener: (_event: ClientEvent, cb: (state: SyncState) => void) => listeners.delete(cb), + } as unknown as MatrixClient; + + const emitSync = (state: SyncState) => { + listeners.forEach((cb) => cb(state)); + }; + + return { mx, checkKeyBackupAndEnable, emitSync, listeners }; +}; + +describe('recheckKeyBackupAfterInitialSync', () => { + it('re-checks key backup once the first sync completes', () => { + const { mx, checkKeyBackupAndEnable, emitSync } = makeKeyBackupMx(null); + + recheckKeyBackupAfterInitialSync(mx); + expect(checkKeyBackupAndEnable).not.toHaveBeenCalled(); + + emitSync(SyncState.Prepared); + + expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1); + }); + + it('re-checks immediately when the client has already synced', () => { + const { mx, checkKeyBackupAndEnable } = makeKeyBackupMx(SyncState.Syncing); + + recheckKeyBackupAfterInitialSync(mx); + + expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1); + }); + + it('re-checks only once and stops listening', () => { + const { mx, checkKeyBackupAndEnable, emitSync, listeners } = makeKeyBackupMx(null); + + recheckKeyBackupAfterInitialSync(mx); + emitSync(SyncState.Prepared); + emitSync(SyncState.Syncing); + + expect(checkKeyBackupAndEnable).toHaveBeenCalledTimes(1); + expect(listeners.size).toBe(0); + }); + + it('waits for a usable sync state', () => { + const { mx, checkKeyBackupAndEnable, emitSync } = makeKeyBackupMx(null); + + recheckKeyBackupAfterInitialSync(mx); + emitSync(SyncState.Reconnecting); + emitSync(SyncState.Error); + + expect(checkKeyBackupAndEnable).not.toHaveBeenCalled(); + }); +}); diff --git a/src/client/initMatrix.ts b/src/client/initMatrix.ts index cd9bc0572..d1c2f87c0 100644 --- a/src/client/initMatrix.ts +++ b/src/client/initMatrix.ts @@ -119,6 +119,27 @@ const startPresenceAfterInitialSync = ( return cleanup; }; +// The SDK's only check runs inside `initRustCrypto`, before the client starts, and latches off. +export const recheckKeyBackupAfterInitialSync = (mx: MatrixClient): void => { + const recheck = () => { + mx.removeListener(ClientEvent.Sync, onSync); + const crypto = mx.getCrypto(); + if (!crypto) return; + crypto.checkKeyBackupAndEnable().catch((error: unknown) => { + debugLog.warn('sync', 'Failed to re-check key backup after initial sync', { + error: error instanceof Error ? error.message : String(error), + }); + }); + }; + + const onSync = (state: SyncState) => { + if (isInitialSyncReady(state)) recheck(); + }; + + if (isInitialSyncReady(mx.getSyncState())) recheck(); + else mx.on(ClientEvent.Sync, onSync); +}; + type StartupPhase = 'sync_store' | 'rust_crypto' | 'client_init' | 'client_start'; const measureStartupPhase = async ( @@ -566,6 +587,7 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig): { transport: useSliding ? 'sliding' : 'classic' } ); if (!useSliding) installSyncStorePersistence(mx); + recheckKeyBackupAfterInitialSync(mx); if (manager && (await manager.waitForSidebarCacheHydration())) { config?.onCachedRoomsLoaded?.(); }