fix(desktop): harden cancellation, dispose, and lifecycle races across main services
Cancel pending result-popup measure timers on hideResultPopup; track CaptionService aux-model load state, stop deduplication, and refine abort epochs across restarts; forward per-call AbortSignal through VoiceModeService and LlmGateway to LocalLLMService and abort pull streams plus availability polling on dispose; clear aborted response pointers and gate stale mic start errors in VoiceConversationService; guard AudioCaptureService stopping resume against exited SoX processes and cleanup failed starts; abort LocalSTTService downloadModel polling on dispose; and fail closed when desktopCapturer.getSources rejects in caption loopback setup.
This commit is contained in:
parent
4bf0f0df12
commit
fc5d45312c
19 changed files with 922 additions and 94 deletions
|
|
@ -256,3 +256,121 @@ describe('LocalLLMService model residency (2026-10-11 memory policy)', () => {
|
|||
expect(unloads).toEqual([{ model: 'gemma4:e4b', keep_alive: 0 }])
|
||||
})
|
||||
})
|
||||
|
||||
describe('LocalLLMService cancellation and dispose safety', () => {
|
||||
beforeEach(() => {
|
||||
initInMemoryConfig()
|
||||
resetLocalLLMServiceForTests()
|
||||
markAvailable()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
resetLocalLLMServiceForTests()
|
||||
resetInMemoryConfig()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('processText_signalAbortedMidGenerate_rejectsWithCancelled', async () => {
|
||||
let seenSignal: AbortSignal | undefined
|
||||
const fetchMock = vi.fn((_input: RequestInfo | URL, init?: RequestInit) => {
|
||||
seenSignal = init?.signal ?? undefined
|
||||
return new Promise<Response>((_resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error('fetch was not aborted')), 300)
|
||||
init?.signal?.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
clearTimeout(timer)
|
||||
reject(new Error('aborted'))
|
||||
},
|
||||
{ once: true },
|
||||
)
|
||||
})
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const controller = new AbortController()
|
||||
const promise = (
|
||||
getLocalLLMService() as unknown as {
|
||||
processText: (
|
||||
text: string,
|
||||
action: 'refine',
|
||||
targetLanguage?: string,
|
||||
customPrompt?: string,
|
||||
options?: { signal?: AbortSignal },
|
||||
) => Promise<string>
|
||||
}
|
||||
).processText('안녕', 'refine', undefined, undefined, { signal: controller.signal })
|
||||
|
||||
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1))
|
||||
controller.abort()
|
||||
|
||||
await expect(promise).rejects.toMatchObject({ code: ErrorCode.LLMProcessingCancelled })
|
||||
expect(seenSignal?.aborted).toBe(true)
|
||||
})
|
||||
|
||||
it('pullModel_disposedMidStream_abortsFetchCancelsReaderAndMarksUnavailable', async () => {
|
||||
let safetyTimer: ReturnType<typeof setTimeout> | null = null
|
||||
const cancelReader = vi.fn(async () => {
|
||||
if (safetyTimer) clearTimeout(safetyTimer)
|
||||
})
|
||||
let seenSignal: AbortSignal | undefined
|
||||
let pullFetchAborted = false
|
||||
|
||||
const fetchMock = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||
seenSignal = init?.signal ?? undefined
|
||||
init?.signal?.addEventListener('abort', () => {
|
||||
pullFetchAborted = true
|
||||
})
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
pull() {
|
||||
return new Promise<void>((_resolve, reject) => {
|
||||
safetyTimer = setTimeout(() => reject(new Error('pull was not cancelled')), 300)
|
||||
})
|
||||
},
|
||||
cancel: cancelReader,
|
||||
})
|
||||
return new Response(stream, { status: 200 })
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const service = getLocalLLMService()
|
||||
const pullPromise = service.pullModel('gemma4:e4b')
|
||||
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1))
|
||||
await new Promise((r) => setTimeout(r, 0))
|
||||
|
||||
service.dispose()
|
||||
|
||||
await expect(pullPromise).rejects.toMatchObject({ code: ErrorCode.LLMProcessingCancelled })
|
||||
expect(pullFetchAborted || Boolean(seenSignal?.aborted)).toBe(true)
|
||||
expect(cancelReader).toHaveBeenCalledTimes(1)
|
||||
expect(service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('checkConnection_disposedWhilePending_doesNotRestoreAvailability', async () => {
|
||||
const service = getLocalLLMService()
|
||||
;(service as unknown as { _available: boolean })._available = false
|
||||
|
||||
let resolveVersion!: (value: Response) => void
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn((input: RequestInfo | URL) => {
|
||||
if (String(input).endsWith('/api/tags')) {
|
||||
return Promise.resolve(new Response(JSON.stringify({ models: [] }), { status: 200 }))
|
||||
}
|
||||
return new Promise<Response>((resolve) => {
|
||||
resolveVersion = resolve
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
const checkPromise = service.checkConnection()
|
||||
await new Promise((r) => setTimeout(r, 0))
|
||||
|
||||
service.dispose()
|
||||
resolveVersion(new Response(JSON.stringify({ version: '0.20.0' }), { status: 200 }))
|
||||
const status = await checkPromise
|
||||
|
||||
expect(status.available).toBe(false)
|
||||
expect(service.isAvailable()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue