From 80e9232692a8d8981e41fe5ebedca038d5d30647 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 11 Aug 2026 03:34:54 +0000 Subject: [PATCH] fix: bound the auth wait in startStoreBootstrap with a timeout Firebase auth resolution was awaited with no timeout, so a stale session or a broken auth response left bootstrap hanging forever with no feedback. Add a 16s timeout (matching the router and WorkspaceAuthGate conventions), retry once after a short backoff, and report a final timeout via Sentry instead of blocking bootstrap indefinitely. --- src/stores/bootstrapStore.test.ts | 53 +++++++++++++++++++++++++++++++ src/stores/bootstrapStore.ts | 53 +++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/src/stores/bootstrapStore.test.ts b/src/stores/bootstrapStore.test.ts index de83713444a..c25da6e48e5 100644 --- a/src/stores/bootstrapStore.test.ts +++ b/src/stores/bootstrapStore.test.ts @@ -68,6 +68,11 @@ const mockDistributionTypes = vi.hoisted(() => ({ })) vi.mock('@/platform/distribution/types', () => mockDistributionTypes) +const mockCaptureException = vi.hoisted(() => vi.fn()) +vi.mock('@sentry/vue', () => ({ + captureException: mockCaptureException +})) + function requestFailure(status: number) { const error = new AxiosError(`Request failed with status code ${status}`) error.response = { status } as AxiosResponse @@ -160,5 +165,53 @@ describe('bootstrapStore', () => { expect(settingStore.isReady).toBe(true) }) }) + + it('retries once and proceeds if auth resolves during the backoff', async () => { + vi.useFakeTimers() + try { + mockIsAuthInitialized.value = true + const store = useBootstrapStore() + const settingStore = useSettingStore() + const bootstrapPromise = store.startStoreBootstrap() + + // First wait times out with the user still unauthenticated. + await vi.advanceTimersByTimeAsync(16_001) + expect(settingStore.isReady).toBe(false) + + // Auth resolves during the retry backoff. + mockIsAuthAuthenticated.value = true + await vi.advanceTimersByTimeAsync(3_001) + await bootstrapPromise + + expect(settingStore.isReady).toBe(true) + expect(mockCaptureException).not.toHaveBeenCalled() + } finally { + vi.useRealTimers() + } + }) + + it('gives up after a second timeout, reports it, and continues bootstrap unauthenticated', async () => { + vi.useFakeTimers() + try { + mockIsAuthInitialized.value = true + const store = useBootstrapStore() + const settingStore = useSettingStore() + const bootstrapPromise = store.startStoreBootstrap() + + // Auth never resolves through the initial wait, the backoff, or the retry. + await vi.advanceTimersByTimeAsync(16_000 + 3_000 + 16_001) + await bootstrapPromise + + expect(mockCaptureException).toHaveBeenCalledOnce() + expect(mockCaptureException).toHaveBeenCalledWith(expect.anything(), { + tags: { error_type: 'bootstrap_auth_wait_timeout' } + }) + // Bootstrap does not stay stuck waiting on auth forever: authenticated + // stores still load rather than leaving the caller hanging silently. + expect(settingStore.isReady).toBe(true) + } finally { + vi.useRealTimers() + } + }) }) }) diff --git a/src/stores/bootstrapStore.ts b/src/stores/bootstrapStore.ts index afc260525e2..703ed500183 100644 --- a/src/stores/bootstrapStore.ts +++ b/src/stores/bootstrapStore.ts @@ -1,3 +1,4 @@ +import { captureException } from '@sentry/vue' import { until, useAsyncState } from '@vueuse/core' import axios from 'axios' import { defineStore, storeToRefs } from 'pinia' @@ -23,6 +24,54 @@ async function fetchCustomNodesI18n(): Promise { } } +// Matches the Firebase-auth-wait timeout used elsewhere (router.ts, +// WorkspaceAuthGate.vue) so a broken/stale session fails this bounded wait +// on the same schedule those already fail theirs. +const AUTH_WAIT_TIMEOUT_MS = 16_000 +const AUTH_WAIT_RETRY_DELAY_MS = 3_000 + +/** + * Waits for Firebase auth to resolve, bounded so a stale session or a + * broken auth response can never hang bootstrap forever. Retries once after + * a short delay; if auth is still unresolved after that, reports it and lets + * bootstrap continue rather than leaving the caller stuck indefinitely. + */ +async function waitForCloudAuth(): Promise { + const { isInitialized, isAuthenticated } = storeToRefs(useAuthStore()) + const waitForResolution = () => + until(isInitialized) + .toBe(true, { timeout: AUTH_WAIT_TIMEOUT_MS, throwOnTimeout: true }) + .then(() => + until(isAuthenticated).toBe(true, { + timeout: AUTH_WAIT_TIMEOUT_MS, + throwOnTimeout: true + }) + ) + + try { + await waitForResolution() + } catch (error) { + console.warn( + '[bootstrapStore] Auth did not resolve in time, retrying once', + error + ) + await new Promise((resolve) => + setTimeout(resolve, AUTH_WAIT_RETRY_DELAY_MS) + ) + try { + await waitForResolution() + } catch (retryError) { + console.error( + '[bootstrapStore] Auth still unresolved after retry; continuing bootstrap without confirmed auth', + retryError + ) + captureException(retryError, { + tags: { error_type: 'bootstrap_auth_wait_timeout' } + }) + } + } +} + export const useBootstrapStore = defineStore('bootstrap', () => { const settingStore = useSettingStore() const workflowStore = useWorkflowStore() @@ -52,9 +101,7 @@ export const useBootstrapStore = defineStore('bootstrap', () => { async function startStoreBootstrap() { if (isCloud) { - const { isInitialized, isAuthenticated } = storeToRefs(useAuthStore()) - await until(isInitialized).toBe(true) - await until(isAuthenticated).toBe(true) + await waitForCloudAuth() } const userStore = useUserStore()