fixes to reduce background client syncs + increase poll timeout to 30s

increase the timeout for background clients to 60s, separates them into their own store, and makes sliding sync respect the setting instead of the client setting, and hopefully better management

adds a startup timeout of 45s to the main loading screen to prevent anything from getting stuck behind that which may or may not actually do anything.

so much writing for such little change :(
This commit is contained in:
7w1 committed 2026-05-04 18:33:05 -05:00
1 parent 3b6fd542fd
commit 7bbcbb841c
3 files changed
+114 -25

No files matched your search

@@ -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<typeof useClientConfig>['slidingSync']
): Promise<MatrixClient> => {
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<typeof setTimeout>[] = [];
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,
+65 -11
View File
@@ -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<MatrixClient> => {
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<MatrixClient> => {
}
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<void> => {
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<void>((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);
+1 -1
View File
@@ -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';