fix(desktop): wait for first-run bootstrap before connection timeout
This commit is contained in:
1 parent
03634b1ca3
commit
b9bb62ea82
6 files changed
+231
-30
No files matched your search
@@ -1,8 +1,67 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { withTimeout } from './with-timeout'
|
||||
import { withBootstrapAwareTimeout, withTimeout } from './with-timeout'
|
||||
|
||||
describe('withTimeout', () => {
|
||||
it.each([{ active: true }, { setupChoice: { active: true } }])(
|
||||
'waits beyond cold boot during setup: %j',
|
||||
async state => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
let finish!: (value: string) => void
|
||||
const connection = new Promise<string>(resolve => {
|
||||
finish = resolve
|
||||
})
|
||||
const probe = vi.fn(async () => state)
|
||||
const result = withBootstrapAwareTimeout(connection, 45_000, 'timeout', probe)
|
||||
await vi.advanceTimersByTimeAsync(180_000)
|
||||
expect(probe).toHaveBeenCalledTimes(4)
|
||||
finish('connected')
|
||||
await expect(result).resolves.toBe('connected')
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
it('keeps an ordinary stalled backend bounded', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const result = withBootstrapAwareTimeout(new Promise<never>(() => {}), 45_000, 'timeout', async () => ({
|
||||
active: false
|
||||
}))
|
||||
const rejection = expect(result).rejects.toThrow('timeout')
|
||||
await vi.advanceTimersByTimeAsync(45_000)
|
||||
await rejection
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('bounds a stalled bootstrap IPC probe', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const result = withBootstrapAwareTimeout(
|
||||
new Promise<never>(() => {}),
|
||||
45_000,
|
||||
'timeout',
|
||||
() => new Promise(() => {})
|
||||
)
|
||||
const rejection = expect(result).rejects.toThrow('timeout')
|
||||
await vi.advanceTimersByTimeAsync(50_000)
|
||||
await rejection
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('propagates a real backend failure during setup', async () => {
|
||||
const failure = new Error('runtime install failed')
|
||||
await expect(
|
||||
withBootstrapAwareTimeout(Promise.reject(failure), 45_000, 'timeout', async () => ({ active: true }))
|
||||
).rejects.toBe(failure)
|
||||
})
|
||||
|
||||
it('rejects with an onTimeout exception instead of letting it escape the timer callback', async () => {
|
||||
vi.useFakeTimers()
|
||||
|
||||
|
||||
@@ -28,6 +28,43 @@ export function isTimeoutError(error: unknown): error is TimeoutError {
|
||||
return error instanceof TimeoutError
|
||||
}
|
||||
|
||||
/** First-run downloads and the user's setup choice are outside the normal
|
||||
* backend cold-start budget. Keep one connection request alive while main
|
||||
* reports that work; a stalled IPC probe and ordinary boots remain bounded. */
|
||||
export async function withBootstrapAwareTimeout<T>(
|
||||
promise: Promise<T>,
|
||||
ms: number,
|
||||
message: string,
|
||||
bootstrapState?: () => Promise<{ active?: boolean; setupChoice?: unknown } | null>,
|
||||
cancelled: () => boolean = () => false
|
||||
): Promise<T> {
|
||||
let settled = false
|
||||
const request = promise.then(
|
||||
value => {
|
||||
settled = true
|
||||
return value
|
||||
},
|
||||
error => {
|
||||
settled = true
|
||||
throw error
|
||||
}
|
||||
)
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
return await withTimeout(request, ms, message)
|
||||
} catch (error) {
|
||||
if (settled || cancelled() || !isTimeoutError(error) || !bootstrapState) {
|
||||
throw error
|
||||
}
|
||||
const state = await withTimeout(bootstrapState(), Math.min(ms, 5_000), message)
|
||||
if (!state?.active && !state?.setupChoice) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Settle with `promise`, or reject with a TimeoutError after `ms`.
|
||||
* `onTimeout` runs synchronously before the rejection is published so callers
|
||||
* can revoke ownership of work that would otherwise keep running unowned. If
|
||||
|
||||
Reference in new issue
Block a user