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:
Yun Chan 2026-11-14 15:06:25 +09:00
parent 4bf0f0df12
commit fc5d45312c
19 changed files with 922 additions and 94 deletions

View file

@ -16,11 +16,12 @@ vi.mock('../../../src/main/services/ConfigService', () => ({
const stt = vi.hoisted(() => ({
gate: null as Promise<void> | null,
initialize: vi.fn(async () => undefined),
releaseAuxModel: vi.fn(async () => undefined),
}))
vi.mock('../../../src/main/services/LocalSTTService', () => ({
getLocalSTTService: () => ({
initialize: vi.fn(async () => undefined),
initialize: stt.initialize,
ensureAuxModel: vi.fn(async () => {
if (stt.gate) await stt.gate
}),
@ -29,7 +30,13 @@ vi.mock('../../../src/main/services/LocalSTTService', () => ({
}),
}))
const audio = vi.hoisted(() => ({ start: vi.fn(async () => undefined), stop: vi.fn(async () => undefined) }))
const audio = vi.hoisted(() => ({
stopGate: null as Promise<void> | null,
start: vi.fn(async () => undefined),
stop: vi.fn(async () => {
if (audio.stopGate) await audio.stopGate
}),
}))
vi.mock('../../../src/main/services/AudioCaptureService', () => ({
getAudioCaptureService: () => ({ ...audio, on: vi.fn(), off: vi.fn() }),
calculateRMS: () => 0.1,
@ -63,7 +70,12 @@ beforeEach(async () => {
vi.clearAllMocks()
config.values = { captionSttModelId: 'caption-small' }
stt.gate = null
stt.initialize.mockImplementation(async () => undefined)
audio.stopGate = null
audio.start.mockImplementation(async () => undefined)
audio.stop.mockImplementation(async () => {
if (audio.stopGate) await audio.stopGate
})
mod = await import('../../../src/main/services/CaptionService')
mod.resetCaptionServiceForTests()
})
@ -98,4 +110,42 @@ describe('CaptionService — 자막 전용 보조 모델 해제', () => {
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
expect(caption.getState()).toBe('inactive')
})
it('releaseAuxModel_auxFinishesLoadingWhileStopping_stillReleasesAuxModel', async () => {
let finishLoad: () => void = () => undefined
stt.gate = new Promise<void>((r) => {
finishLoad = r
})
const caption = mod.getCaptionService()
const starting = caption.start({ audioSource: 'mic', showOverlay: false })
await new Promise((r) => setTimeout(r, 0))
// stop() immediately transitions state to 'stopping' before yielding to awaits
const stopping = caption.stop()
expect(caption.getState()).toBe('stopping')
// Aux model finishes loading on the same tick while stop() is still in 'stopping'
stt.releaseAuxModel.mockClear()
finishLoad()
await expect(starting).rejects.toBeTruthy()
await stopping
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
expect(caption.getState()).toBe('inactive')
})
it('start_failsBeforeAuxLoadWhenPreviousSessionHadAux_doesNotReleaseSpuriously', async () => {
audio.start.mockRejectedValueOnce(new Error('no mic'))
const caption = mod.getCaptionService()
await expect(caption.start({ audioSource: 'mic', showOverlay: false })).rejects.toBeTruthy()
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
stt.releaseAuxModel.mockClear()
config.values = {}
stt.initialize.mockRejectedValueOnce(new Error('sidecar down'))
await expect(caption.start({ audioSource: 'mic', showOverlay: false })).rejects.toBeTruthy()
expect(stt.releaseAuxModel).not.toHaveBeenCalled()
})
})