Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/stores/bootstrapStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
})
})
})
53 changes: 50 additions & 3 deletions src/stores/bootstrapStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { captureException } from '@sentry/vue'
import { until, useAsyncState } from '@vueuse/core'
import axios from 'axios'
import { defineStore, storeToRefs } from 'pinia'
Expand All @@ -23,6 +24,54 @@ async function fetchCustomNodesI18n(): Promise<CustomNodesI18n | undefined> {
}
}

// 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<void> {
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()
Expand Down Expand Up @@ -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()
Expand Down
Loading