From 02172cc9981bdc6fb9ddd29b040283ec98710b93 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Thu, 23 Jul 2026 17:47:46 +0200 Subject: [PATCH] fix: stop leaking session_started/session_ended listeners per room useCallSession subscribed to mx.matrixRTC.on('session_started'/'session_ended') once per call, and is used by RoomNavItem (one per visible room). With 11+ visible rooms this exceeded Node's default EventEmitter limit of 10 and stacked O(N) listeners that each did O(1) filtering per event. Move the subscription into a single MatrixRTCSessionProvider that exposes active RTC session room IDs via React context. useCallSession now reads from that context instead of subscribing per-room. --- src/app/hooks/useCall.ts | 30 ++------ src/app/hooks/useMatrixRTCSession.tsx | 80 ++++++++++++++++++++ src/app/pages/client/ClientNonUIFeatures.tsx | 5 +- src/client/slidingSync.ts | 2 +- 4 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 src/app/hooks/useMatrixRTCSession.tsx diff --git a/src/app/hooks/useCall.ts b/src/app/hooks/useCall.ts index 792d39e01..500b96233 100644 --- a/src/app/hooks/useCall.ts +++ b/src/app/hooks/useCall.ts @@ -1,33 +1,17 @@ import type { Room } from '$types/matrix-sdk'; import { MatrixRTCSession, MatrixRTCSessionEvent } from '$types/matrix-sdk'; import type { CallMembership } from '$types/matrix-sdk'; -import { useEffect, useState } from 'react'; -import { MatrixRTCSessionManagerEvents } from '$types/matrix-sdk'; +import { useEffect, useMemo, useState } from 'react'; import { useMatrixClient } from './useMatrixClient'; +import { useActiveRTCSessionIds } from './useMatrixRTCSession'; export const useCallSession = (room: Room): MatrixRTCSession => { const mx = useMatrixClient(); - - const [session, setSession] = useState(mx.matrixRTC.getRoomSession(room)); - - useEffect(() => { - const start = (roomId: string) => { - if (roomId !== room.roomId) return; - setSession(mx.matrixRTC.getRoomSession(room)); - }; - const end = (roomId: string) => { - if (roomId !== room.roomId) return; - setSession(mx.matrixRTC.getRoomSession(room)); - }; - mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, start); - mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, end); - return () => { - mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, start); - mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, end); - }; - }, [mx, room]); - - return session; + const activeRoomIds = useActiveRTCSessionIds(); + // Re-derive the session whenever the active-session set changes. The returned + // session object is stable across renders unless the set changes. + // eslint-disable-next-line react-hooks/exhaustive-deps + return useMemo(() => mx.matrixRTC.getRoomSession(room), [mx, room, activeRoomIds]); }; export const useCallMembers = (room: Room, session: MatrixRTCSession): CallMembership[] => { diff --git a/src/app/hooks/useMatrixRTCSession.tsx b/src/app/hooks/useMatrixRTCSession.tsx new file mode 100644 index 000000000..c7255ab5a --- /dev/null +++ b/src/app/hooks/useMatrixRTCSession.tsx @@ -0,0 +1,80 @@ +import type { ReactElement, ReactNode } from 'react'; +import { createContext, useContext, useEffect, useState } from 'react'; +import { MatrixRTCSessionManagerEvents } from '$types/matrix-sdk'; +import { useMatrixClient } from './useMatrixClient'; + +/** + * The set of room IDs that currently have an active MatrixRTC session. + * Updated by a single global subscription to `mx.matrixRTC`, so N consumers + * share one listener pair instead of each adding their own (which exceeded + * Node's default EventEmitter limit of 10 when 11+ rooms were visible). + */ +type ActiveRTCSessionIds = ReadonlySet; + +const MatrixRTCSessionContext = createContext(new Set()); + +export function MatrixRTCSessionProvider({ children }: { children: ReactNode }): ReactElement { + const mx = useMatrixClient(); + const [activeRoomIds, setActiveRoomIds] = useState(() => { + // Seed with rooms that currently have an active session. + // There is no public `getRoomSessions()` bulk method on MatrixRTCSessionManager, + // so we iterate all client rooms and check each one individually. + const ids = new Set(); + try { + for (const room of mx.getRooms()) { + try { + const session = mx.matrixRTC.getRoomSession(room); + if (session.memberships.length > 0) { + ids.add(room.roomId); + } + } catch { + // Individual room session lookup failure is non-fatal. + } + } + } catch { + // Best-effort seed; the subscription below will keep it accurate. + } + return ids; + }); + + useEffect(() => { + const handleStart = (roomId: string) => { + setActiveRoomIds((prev) => { + if (prev.has(roomId)) return prev; + const next = new Set(prev); + next.add(roomId); + return next; + }); + }; + const handleEnd = (roomId: string) => { + setActiveRoomIds((prev) => { + if (!prev.has(roomId)) return prev; + const next = new Set(prev); + next.delete(roomId); + return next; + }); + }; + + mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, handleStart); + mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, handleEnd); + + return () => { + mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, handleStart); + mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, handleEnd); + }; + }, [mx]); + + return ( + + {children} + + ); +} + +/** + * Returns the set of room IDs that currently have an active MatrixRTC session. + * Backed by a single global subscription (see MatrixRTCSessionProvider). + */ +export function useActiveRTCSessionIds(): ActiveRTCSessionIds { + return useContext(MatrixRTCSessionContext); +} diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index b39ec1512..1881ba3c4 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -65,6 +65,7 @@ import { NotificationBanner } from '$components/notification-banner'; import { ThemeMigrationBanner } from '$components/theme/ThemeMigrationBanner'; import { TelemetryConsentBanner } from '$components/telemetry-consent'; import { useIncomingCallSignaling } from '$hooks/useCallSignaling'; +import { MatrixRTCSessionProvider } from '$hooks/useMatrixRTCSession'; import { lastVisitedRoomAtom } from '$state/room/lastRoom'; import { useSettingsSyncEffect } from '$hooks/useSettingsSync'; import { resolveIncomingCallFromNotificationData } from '$features/call/callNotificationBridge'; @@ -1004,7 +1005,7 @@ function NativeNotificationClickRouting() { export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) { useIncomingCallSignaling(); return ( - <> + @@ -1032,6 +1033,6 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) { {children} - + ); } diff --git a/src/client/slidingSync.ts b/src/client/slidingSync.ts index 08925cf58..0c29ed10b 100644 --- a/src/client/slidingSync.ts +++ b/src/client/slidingSync.ts @@ -1006,7 +1006,7 @@ export class SlidingSyncManager { */ private reassertOptimisticJoins(): void { if (this.optimisticallyJoinedRoomIds.size === 0) return; - for (const roomId of [...this.optimisticallyJoinedRoomIds]) { + for (const roomId of this.optimisticallyJoinedRoomIds) { const room = this.mx.getRoom(roomId); if (!room) { this.optimisticallyJoinedRoomIds.delete(roomId);