diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index 35c196348..79be8d1b4 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -3,6 +3,7 @@ import type { MatrixClient, MatrixEvent, Room } from '$types/matrix-sdk'; import { ClientEvent, createClient, + IndexedDBStore, MatrixEventEvent, RoomEvent, SyncState, @@ -47,6 +48,10 @@ import { mobileOrTablet } from '$utils/user-agent'; const log = createLogger('BackgroundNotifications'); const debugLog = createDebugLogger('BackgroundNotifications'); + +const BACKGROUND_SYNC_POLL_TIMEOUT_MS = 60_000; +const BACKGROUND_STAGGER_DELAY_MS = 5_000; + const isClientReadyForNotifications = (state: SyncState | string | null): boolean => state === SyncState.Prepared || state === SyncState.Syncing || state === SyncState.Catchup; @@ -54,18 +59,36 @@ const startBackgroundClient = async ( session: Session, slidingSyncConfig: ReturnType['slidingSync'] ): Promise => { + const storeName = { + sync: `bg-sync${session.userId}`, + crypto: `bg-crypto${session.userId}`, + rustCryptoPrefix: `bg-sync${session.userId}`, + }; + + const indexedDBStore = new IndexedDBStore({ + indexedDB: global.indexedDB, + localStorage: global.localStorage, + dbName: storeName.sync, + }); + const mx = createClient({ baseUrl: session.baseUrl, accessToken: session.accessToken, userId: session.userId, deviceId: session.deviceId, + store: indexedDBStore, timelineSupport: false, }); - await startClient(mx, { + + const startOpts = { baseUrl: session.baseUrl, - slidingSync: slidingSyncConfig, + slidingSync: session.slidingSyncOptIn ? slidingSyncConfig : undefined, sessionSlidingSyncOptIn: session.slidingSyncOptIn, - }); + pollTimeoutMs: BACKGROUND_SYNC_POLL_TIMEOUT_MS, + timelineLimit: 1, + }; + + await startClient(mx, startOpts); return mx; }; @@ -136,9 +159,11 @@ export function BackgroundNotifications() { // listeners before stopping a background client. const clientCleanupRef = useRef(new Map()); + const activeUserId = activeSessionId ?? sessions[0]?.userId; + const inactiveSessions = useMemo( - () => sessions.filter((s) => s.userId !== (activeSessionId ?? sessions[0]?.userId)), - [sessions, activeSessionId] + () => sessions.filter((s) => s.userId !== activeUserId), + [sessions, activeUserId] ); // Ref so retry setTimeout callbacks can access the current session list // without stale closures. @@ -540,20 +565,30 @@ export function BackgroundNotifications() { }); }; - inactiveSessions.forEach((session) => { - if (!current.has(session.userId)) startSession(session); + const pendingSessions = inactiveSessions.filter((s) => !current.has(s.userId)); + const staggerTimers: ReturnType[] = []; + pendingSessions.forEach((session, idx) => { + if (idx === 0) { + startSession(session); + } else { + staggerTimers.push( + setTimeout(() => startSession(session), idx * BACKGROUND_STAGGER_DELAY_MS) + ); + } }); - // Capture the cleanup map for this effect instance so teardown runs against - // the listeners registered while this effect was active. const cleanupMap = clientCleanupRef.current; + const activeUserIds = new Set(inactiveSessions.map((s) => s.userId)); return () => { + staggerTimers.forEach(clearTimeout); current.forEach((mx, userId) => { - cleanupMap.get(userId)?.(); - cleanupMap.delete(userId); - stopClient(mx); + if (!activeUserIds.has(userId)) { + cleanupMap.get(userId)?.(); + cleanupMap.delete(userId); + stopClient(mx); + current.delete(userId); + } }); - current.clear(); }; }, [ clientConfig.slidingSync, diff --git a/src/client/initMatrix.ts b/src/client/initMatrix.ts index 25249718c..6a7ba0043 100644 --- a/src/client/initMatrix.ts +++ b/src/client/initMatrix.ts @@ -2,6 +2,7 @@ import type { CryptoCallbacks, MatrixClient, ISyncStateData } from '$types/matri import { ClientEvent, createClient, + Filter, IndexedDBStore, IndexedDBCryptoStore, SyncState, @@ -26,7 +27,7 @@ const classicSyncObserverByClient = new WeakMap< MatrixClient, (state: SyncState, prevState: SyncState | null, data?: ISyncStateData) => void >(); -const FAST_SYNC_POLL_TIMEOUT_MS = 10000; +const FAST_SYNC_POLL_TIMEOUT_MS = 30_000; const SLIDING_SYNC_POLL_TIMEOUT_MS = 20000; type SyncTransport = 'classic' | 'sliding'; type SyncTransportReason = @@ -315,7 +316,9 @@ export const initClient = async (session: Session): Promise => { const wipeAllStores = async () => { log.warn('initClient: wiping all stores for', session.userId); - debugLog.warn('sync', 'Wiping all stores due to mismatch', { userId: session.userId }); + debugLog.warn('sync', 'Wiping all stores due to mismatch', { + userId: session.userId, + }); Sentry.addBreadcrumb({ category: 'crypto', message: 'Crypto store mismatch — wiping local stores and retrying', @@ -353,18 +356,24 @@ export const initClient = async (session: Session): Promise => { } try { - await mx.initRustCrypto({ cryptoDatabasePrefix: storeName.rustCryptoPrefix }); + await mx.initRustCrypto({ + cryptoDatabasePrefix: storeName.rustCryptoPrefix, + }); } catch (err) { if (!isMismatch(err)) { debugLog.error('sync', 'Failed to initialize crypto', { error: err }); throw err; } log.warn('initClient: mismatch on initRustCrypto — wiping and retrying:', err); - debugLog.warn('sync', 'Crypto init mismatch - wiping stores and retrying', { error: err }); + debugLog.warn('sync', 'Crypto init mismatch - wiping stores and retrying', { + error: err, + }); mx.stopClient(); await wipeAllStores(); mx = await buildClient(session); - await mx.initRustCrypto({ cryptoDatabasePrefix: storeName.rustCryptoPrefix }); + await mx.initRustCrypto({ + cryptoDatabasePrefix: storeName.rustCryptoPrefix, + }); } mx.setMaxListeners(50); @@ -375,6 +384,8 @@ export type StartClientConfig = { baseUrl?: string; slidingSync?: SlidingSyncConfig; sessionSlidingSyncOptIn?: boolean; + pollTimeoutMs?: number; + timelineLimit?: number; }; export type ClientSyncDiagnostics = SyncTransportMeta & { @@ -415,7 +426,12 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig): hasProxy: hasSlidingProxy, }); - const startClassicSync = async (fallbackFromSliding: boolean, reason: SyncTransportReason) => { + const CLASSIC_SYNC_STARTUP_TIMEOUT_MS = 45_000; + + const startClassicSync = async ( + fallbackFromSliding: boolean, + reason: SyncTransportReason + ): Promise => { syncTransportByClient.set(mx, { transport: 'classic', slidingConfigured: slidingEnabledOnServer, @@ -426,13 +442,44 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig): reason, }); Sentry.metrics.count('sable.sync.transport', 1, { - attributes: { transport: 'classic', reason, fallback: String(fallbackFromSliding) }, + attributes: { + transport: 'classic', + reason, + fallback: String(fallbackFromSliding), + }, }); - await mx.startClient({ + + const startupTimeout = new Promise((resolve) => { + window.setTimeout(() => { + debugLog.warn('sync', 'Classic sync startup timed out', { + userId: mx.getUserId(), + timeoutMs: CLASSIC_SYNC_STARTUP_TIMEOUT_MS, + }); + resolve(); + }, CLASSIC_SYNC_STARTUP_TIMEOUT_MS); + }); + + const effectivePollTimeout = config?.pollTimeoutMs ?? FAST_SYNC_POLL_TIMEOUT_MS; + const effectiveTimelineLimit = config?.timelineLimit ?? 10; + + const classicFilter = new Filter(mx.getUserId() ?? undefined); + classicFilter.setTimelineLimit(effectiveTimelineLimit); + // Ensure lazy loading stays on (carried by buildDefaultFilter but explicit here + // since we replace the filter entirely rather than merging). + const filterDefinition = classicFilter.getDefinition(); + if (filterDefinition.room) { + filterDefinition.room.timeline = filterDefinition.room.timeline ?? {}; + (filterDefinition.room.timeline as { lazy_load_members?: boolean }).lazy_load_members = true; + } + + const syncStarted = mx.startClient({ lazyLoadMembers: true, - pollTimeout: FAST_SYNC_POLL_TIMEOUT_MS, + pollTimeout: effectivePollTimeout, threadSupport: true, + filter: classicFilter, }); + + await Promise.race([syncStarted, startupTimeout]); // Attach an ongoing classic-sync observer — equivalent to SlidingSyncManager's // onLifecycle listener. Tracks state transitions, initial-sync timing, and errors. let classicSyncCount = 0; @@ -584,7 +631,11 @@ export const startClient = async (mx: MatrixClient, config?: StartClientConfig): reason: 'sliding_active', }); Sentry.metrics.count('sable.sync.transport', 1, { - attributes: { transport: 'sliding', reason: 'sliding_active', fallback: 'false' }, + attributes: { + transport: 'sliding', + reason: 'sliding_active', + fallback: 'false', + }, }); try { @@ -649,7 +700,10 @@ export const getClientSyncDiagnostics = (mx: MatrixClient): ClientSyncDiagnostic * so the correct Jotai Provider store is used. */ export const logoutClient = async (mx: MatrixClient, session?: Session) => { - log.log('logoutClient', { userId: mx.getUserId(), sessionUserId: session?.userId }); + log.log('logoutClient', { + userId: mx.getUserId(), + sessionUserId: session?.userId, + }); debugLog.info('general', 'Logging out client', { userId: mx.getUserId() }); pushSessionToSW(); stopClient(mx); diff --git a/src/types/matrix-sdk.ts b/src/types/matrix-sdk.ts index 6a51b180a..d05073f2e 100644 --- a/src/types/matrix-sdk.ts +++ b/src/types/matrix-sdk.ts @@ -12,7 +12,7 @@ export * from 'matrix-js-sdk/lib/sliding-sync'; export * from 'matrix-js-sdk/lib/sync-accumulator'; export * from 'matrix-js-sdk/lib/scheduler'; export * from 'matrix-js-sdk/lib/store/memory'; -export { createClient } from 'matrix-js-sdk/lib/matrix'; +export { createClient, Filter } from 'matrix-js-sdk/lib/matrix'; export * from 'matrix-js-sdk/lib/models/event'; export * from 'matrix-js-sdk/lib/models/room';