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
|
|
@ -882,6 +882,15 @@ class LocalLLMService extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`Pull 완료: ${modelId}`)
|
logger.info(`Pull 완료: ${modelId}`)
|
||||||
|
} catch (err) {
|
||||||
|
if (this._disposed || controller.signal.aborted) {
|
||||||
|
throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`)
|
||||||
|
}
|
||||||
|
if (err instanceof D3ROError) throw err
|
||||||
|
throw new D3ROError(
|
||||||
|
ErrorCode.LLMServerUnreachable,
|
||||||
|
`Pull 실패: ${err instanceof Error ? err.message : String(err)}`
|
||||||
|
)
|
||||||
} finally {
|
} finally {
|
||||||
controller.signal.removeEventListener('abort', onAbort)
|
controller.signal.removeEventListener('abort', onAbort)
|
||||||
this._pullControllers.delete(controller)
|
this._pullControllers.delete(controller)
|
||||||
|
|
|
||||||
|
|
@ -551,6 +551,9 @@ class LocalSTTService extends EventEmitter {
|
||||||
|
|
||||||
if (startRes.ok) {
|
if (startRes.ok) {
|
||||||
const started = (await startRes.json()) as { status: string }
|
const started = (await startRes.json()) as { status: string }
|
||||||
|
if (this._disposed) {
|
||||||
|
throw new D3ROError(ErrorCode.STTModelDownloadCancelled, `모델 다운로드 취소: ${modelId}`)
|
||||||
|
}
|
||||||
if (started.status === 'done') {
|
if (started.status === 'done') {
|
||||||
this._emitDownloadProgress(modelId, 100, 0, 0, 0)
|
this._emitDownloadProgress(modelId, 100, 0, 0, 0)
|
||||||
return
|
return
|
||||||
|
|
@ -1047,10 +1050,27 @@ class LocalSTTService extends EventEmitter {
|
||||||
}
|
}
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
this._downloadControllers.add(controller)
|
this._downloadControllers.add(controller)
|
||||||
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
let timedOut = false
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
timedOut = true
|
||||||
|
controller.abort()
|
||||||
|
}, timeoutMs)
|
||||||
timer.unref?.()
|
timer.unref?.()
|
||||||
try {
|
try {
|
||||||
return await fetch(url, { ...init, signal: controller.signal })
|
const response = await fetch(url, { ...init, signal: controller.signal })
|
||||||
|
if (this._disposed) {
|
||||||
|
throw new D3ROError(ErrorCode.STTModelDownloadCancelled, 'LocalSTTService가 dispose되었습니다')
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
} catch (err) {
|
||||||
|
if (this._disposed || (!timedOut && controller.signal.aborted)) {
|
||||||
|
throw new D3ROError(ErrorCode.STTModelDownloadCancelled, 'LocalSTTService가 dispose되었습니다')
|
||||||
|
}
|
||||||
|
if (err instanceof D3ROError) throw err
|
||||||
|
throw new D3ROError(
|
||||||
|
ErrorCode.STTModelDownloadFailed,
|
||||||
|
`다운로드 요청 실패: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
|
)
|
||||||
} finally {
|
} finally {
|
||||||
clearTimeout(timer)
|
clearTimeout(timer)
|
||||||
this._downloadControllers.delete(controller)
|
this._downloadControllers.delete(controller)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
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 { configSet, initInMemoryConfig, resetInMemoryConfig } from '../../../src/main/services/ConfigService'
|
||||||
import { availableParallelism } from 'node:os'
|
import { availableParallelism } from 'node:os'
|
||||||
import { inferenceCpuThreads } from '../../../src/main/utils/inference-cpu-budget'
|
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(status.available).toBe(false)
|
||||||
expect(service.isAvailable()).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
|
await disposePromise
|
||||||
expect(statusPollsAfterDispose).toBe(0)
|
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