fix(desktop): wrap pull and STT download aborts into D3ROError on dispose
Catch pre-header fetch and stream rejections in LocalLLMService._doPullModel and map disposed or aborted pulls to ErrorCode.LLMProcessingCancelled (and unexpected failures to ErrorCode.LLMServerUnreachable). Convert disposed fetch aborts and JSON body aborts in LocalSTTService._fetchDownload and downloadModel into ErrorCode.STTModelDownloadCancelled while wrapping network or timeout errors as ErrorCode.STTModelDownloadFailed.
This commit is contained in:
parent
fc5d45312c
commit
3f9e412257
4 changed files with 103 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { ErrorCode } from '@d3ro/core/errors'
|
||||
import { D3ROError, ErrorCode } from '@d3ro/core/errors'
|
||||
import { configSet, initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
||||
import { availableParallelism } from 'node:os'
|
||||
import { inferenceCpuThreads } from '../../../src/main/utils/inference-cpu-budget'
|
||||
|
|
@ -373,4 +373,35 @@ describe('LocalLLMService cancellation and dispose safety', () => {
|
|||
expect(status.available).toBe(false)
|
||||
expect(service.isAvailable()).toBe(false)
|
||||
})
|
||||
|
||||
it('pullModel_disposedBeforeHeadersArrive_rejectsWithLLMProcessingCancelled', 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('pull fetch was not aborted')), 300)
|
||||
init?.signal?.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
clearTimeout(timer)
|
||||
reject(new DOMException('The operation was aborted.', 'AbortError'))
|
||||
},
|
||||
{ once: true },
|
||||
)
|
||||
})
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const service = getLocalLLMService()
|
||||
const pullPromise = service.pullModel('gemma4:e4b')
|
||||
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1))
|
||||
|
||||
service.dispose()
|
||||
|
||||
await expect(pullPromise).rejects.toSatisfy(
|
||||
(err: unknown) => err instanceof D3ROError && err.code === ErrorCode.LLMProcessingCancelled,
|
||||
)
|
||||
expect(seenSignal?.aborted).toBe(true)
|
||||
expect(service.isAvailable()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -424,4 +424,44 @@ describe('LocalSTTService — 사이드카 크래시 재시작', () => {
|
|||
await disposePromise
|
||||
expect(statusPollsAfterDispose).toBe(0)
|
||||
})
|
||||
|
||||
it('downloadModel_disposedWhileStartRequestPending_rejectsWithSTTModelDownloadCancelled', async () => {
|
||||
const h = createHarness()
|
||||
h.setHealthy(true)
|
||||
let startSignal: AbortSignal | undefined
|
||||
|
||||
const wrappedFetch = vi.fn((input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input)
|
||||
if (url.endsWith('/download')) {
|
||||
startSignal = init?.signal ?? undefined
|
||||
return new Promise<Response>((_resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error('download start fetch was not aborted')), 300)
|
||||
init?.signal?.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
clearTimeout(timer)
|
||||
reject(new DOMException('The operation was aborted.', 'AbortError'))
|
||||
},
|
||||
{ once: true },
|
||||
)
|
||||
})
|
||||
}
|
||||
return h.fetch(url, init)
|
||||
})
|
||||
vi.stubGlobal('fetch', wrappedFetch)
|
||||
|
||||
const local = new LocalSTTService({ supervisor: h.supervisor })
|
||||
const downloadPromise = local.downloadModel('tiny')
|
||||
await vi.waitFor(() =>
|
||||
expect(wrappedFetch.mock.calls.some(([url]) => String(url).endsWith('/download'))).toBe(true),
|
||||
)
|
||||
|
||||
const disposePromise = local.dispose()
|
||||
|
||||
await expect(downloadPromise).rejects.toSatisfy(
|
||||
(err: unknown) => err instanceof D3ROError && err.code === ErrorCode.STTModelDownloadCancelled,
|
||||
)
|
||||
await disposePromise
|
||||
expect(startSignal?.aborted).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue