From 8e6e77dd4c6b9716d7dd9e988dbbcdf4ac957159 Mon Sep 17 00:00:00 2001 From: Dotta Date: Tue, 10 Nov 2026 10:53:00 -0600 Subject: [PATCH] fix(ui): hold the socket auth scope in state, require one session read Review follow-ups. The previous sign-in scope now lives in state and is adjusted during render, instead of a ref written while rendering. The returning-visit browser check requires exactly one session read, so it fails if the session is no longer verified. Co-Authored-By: Claude Opus 5.5 --- tests/perf/collections/shell-read-waves.spec.ts | 2 ++ ui/src/context/LiveUpdatesProvider.tsx | 15 ++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/perf/collections/shell-read-waves.spec.ts b/tests/perf/collections/shell-read-waves.spec.ts index fa253ba4768..76e1555906a 100644 --- a/tests/perf/collections/shell-read-waves.spec.ts +++ b/tests/perf/collections/shell-read-waves.spec.ts @@ -25,6 +25,8 @@ test("a returning visit makes each shell read at most once and keeps one live so await rowsAt(); await page.waitForTimeout(5_000); const counts = shellReadCounts(); + // The restored session is verified exactly once; that read is what used to reconnect the socket. + expect(counts["auth/get-session"], JSON.stringify(counts)).toBe(1); expect(Object.entries(counts).filter(([, count]) => count > 1), JSON.stringify(counts)).toEqual([]); expect(sockets, "The verified session reconnected the live socket").toHaveLength(1); } finally { await context.close(); } diff --git a/ui/src/context/LiveUpdatesProvider.tsx b/ui/src/context/LiveUpdatesProvider.tsx index 0f2bc64e5e6..a61f3253f00 100644 --- a/ui/src/context/LiveUpdatesProvider.tsx +++ b/ui/src/context/LiveUpdatesProvider.tsx @@ -2235,10 +2235,8 @@ type SocketAuthScope = { userId: string | null; sessionId: string | null; key: s * reconnects. */ function nextSocketAuthScope(previous: SocketAuthScope | null, userId: string | null, sessionId: string | null): SocketAuthScope { - if (previous && userId !== null && previous.userId === userId) { - if (previous.sessionId === sessionId) return previous; - if (previous.sessionId === null) return { ...previous, sessionId }; - } + if (previous && previous.userId === userId && previous.sessionId === sessionId) return previous; + if (previous && userId !== null && previous.userId === userId && previous.sessionId === null) return { ...previous, sessionId }; return { userId, sessionId, key: sessionId ?? userId ?? "signed_out" }; } @@ -2269,9 +2267,12 @@ export function LiveUpdatesProvider({ children }: { children: ReactNode }) { const { data: health } = useQuery({ queryKey: queryKeys.health, queryFn: healthApi.get }); const currentUserId = session?.user?.id ?? session?.session?.userId ?? (health?.deploymentMode === "local_trusted" ? "local-board" : null); - const socketAuth = useRef(null); - socketAuth.current = nextSocketAuthScope(socketAuth.current, currentUserId, session?.session?.id ?? null); - const socketAuthKey = socketAuth.current.key; + const sessionId = session?.session?.id ?? null; + const [socketAuth, setSocketAuth] = useState(() => nextSocketAuthScope(null, currentUserId, sessionId)); + const nextSocketAuth = nextSocketAuthScope(socketAuth, currentUserId, sessionId); + // Adjust state during render: the scope depends on the previous scope. + if (nextSocketAuth !== socketAuth) setSocketAuth(nextSocketAuth); + const socketAuthKey = nextSocketAuth.key; const [connectedScopeKey, setConnectedScopeKey] = useState(null); const liveCompanyId = resolveLiveCompanyId(selectedCompanyId, selectedCompany?.id ?? null); const socketScopeKey = JSON.stringify([liveCompanyId, socketAuthKey]);