From fc5d45312c1d9a29dfcd5324c26fb37c6b8918ee Mon Sep 17 00:00:00 2001 From: Yun Chan Date: Sat, 14 Nov 2026 15:06:25 +0900 Subject: [PATCH] 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. --- apps/desktop/src/main/ipc/caption-handlers.ts | 18 +- .../src/main/services/AudioCaptureService.ts | 37 +++- .../src/main/services/CaptionService.ts | 84 ++++++++- .../src/main/services/LocalLLMService.ts | 158 +++++++++++----- .../src/main/services/LocalSTTService.ts | 92 ++++++++- .../main/services/VoiceConversationService.ts | 14 +- .../src/main/services/VoiceModeService.ts | 17 +- .../src/main/services/llm/LlmGateway.ts | 14 +- .../desktop/src/main/windows/WindowManager.ts | 2 + .../tests/main/ipc/ipc-redteam-r3-1.test.ts | 23 +++ .../services/VoiceConversationService.test.ts | 60 ++++++ .../main/services/VoiceModeService.test.ts | 30 +++ .../audio-capture-redteam-r2-7.test.ts | 35 ++++ .../main/services/caption-aux-release.test.ts | 54 +++++- .../services/caption-lifecycle-refine.test.ts | 178 ++++++++++++++++++ .../llm-limits-routing-redteam-r2-1.test.ts | 13 ++ .../main/services/local-llm-service.test.ts | 118 ++++++++++++ .../services/stt-sidecar-redteam-r2-6.test.ts | 50 +++++ .../main/windows/popup-hide-show-race.test.ts | 19 ++ 19 files changed, 922 insertions(+), 94 deletions(-) create mode 100644 apps/desktop/tests/main/services/caption-lifecycle-refine.test.ts diff --git a/apps/desktop/src/main/ipc/caption-handlers.ts b/apps/desktop/src/main/ipc/caption-handlers.ts index c4c71e0..fa10cce 100644 --- a/apps/desktop/src/main/ipc/caption-handlers.ts +++ b/apps/desktop/src/main/ipc/caption-handlers.ts @@ -5,6 +5,7 @@ import { ipcMain, session, desktopCapturer } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { getCaptionService } from '../services/CaptionService' +import { getLogger } from '../services/LoggerService' import type { CaptionConfig } from '@d3ro/core/types' import { endCaptionOverlayDrag, @@ -13,6 +14,8 @@ import { startCaptionOverlayDrag } from '../windows/WindowManager' +const logger = getLogger('CaptionHandlers') + export function registerCaptionHandlers(): void { // ── 자막 창 손잡이 (팝업 → 메인) ── ipcMain.on(IPC_CHANNELS.POPUP_CAPTION.SET_INTERACTIVE, (_event, interactive: unknown) => { @@ -25,12 +28,19 @@ export function registerCaptionHandlers(): void { // 시스템 오디오 루프백: setDisplayMediaRequestHandler로 audio: 'loopback' 설정 ipcMain.handle(IPC_CHANNELS.SYSTEM_AUDIO.ENABLE_LOOPBACK, async () => { session.defaultSession.setDisplayMediaRequestHandler(async (_request, callback) => { - const sources = await desktopCapturer.getSources({ types: ['screen'] }) - if (sources.length === 0) { + try { + const sources = await desktopCapturer.getSources({ types: ['screen'] }) + if (sources.length === 0) { + callback({}) + return + } + callback({ video: sources[0], audio: 'loopback' }) + } catch (err) { + logger.warn( + `Display media source lookup failed: ${err instanceof Error ? err.message : String(err)}`, + ) callback({}) - return } - callback({ video: sources[0], audio: 'loopback' }) }) return ipcSuccess(undefined) }) diff --git a/apps/desktop/src/main/services/AudioCaptureService.ts b/apps/desktop/src/main/services/AudioCaptureService.ts index e1cfc91..75bcfa6 100644 --- a/apps/desktop/src/main/services/AudioCaptureService.ts +++ b/apps/desktop/src/main/services/AudioCaptureService.ts @@ -94,6 +94,7 @@ class AudioCaptureService extends EventEmitter { private _refCount = 0 private _cachedDevices: AudioDevice[] | null = null private _soxProcess: ChildProcess | null = null + private _soxExited = false private _stream: Readable | null = null private _levelInterval: ReturnType | null = null /** 새 SoX 를 띄울 때마다 증가 — 임대(lease)가 자기 캡처인지 판단한다 */ @@ -155,15 +156,21 @@ class AudioCaptureService extends EventEmitter { return } - // 마지막 참조가 꼬리를 받는 중에 새 소비자가 오면 정지를 취소하고 같은 SoX 로 이어 간다 + // 마지막 참조가 꼬리를 받는 중에 새 소비자가 오면 SoX가 아직 살아 있을 때만 정지를 취소하고 이어 간다 if (this._state === 'stopping' && this._pendingStopToken !== 0) { this._pendingStopToken = 0 this._cancelTailWait?.() - this._state = 'capturing' - this._refCount = 1 - this._startLevelTimer() - logger.debug('Pending stop cancelled by a new consumer — capture continues') - return + if (this._isSoxProcessAlive()) { + this._state = 'capturing' + this._refCount = 1 + this._startLevelTimer() + logger.debug('Pending stop cancelled by a new consumer — capture continues') + return + } + this._emitResidual() + this._cleanup() + this._currentDevice = null + this._state = 'idle' } if (this._state !== 'idle' && this._state !== 'error') { @@ -202,6 +209,7 @@ class AudioCaptureService extends EventEmitter { logger.info(`SoX args: ${soxArgs.join(' ')}`) const child = spawn(soxExe, soxArgs, { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }) this._soxProcess = child + this._soxExited = false this._generation++ this._stream = child.stdout this._residualBuffer = Buffer.alloc(0) @@ -222,11 +230,18 @@ class AudioCaptureService extends EventEmitter { if (msg) logger.warn(`SoX stderr: ${msg}`) }) + child.once('exit', () => { + if (this._soxProcess === child) { + this._soxExited = true + } + }) + // SoX 프로세스 종료 감지. // 자기 프로세스인지 먼저 확인한다 — 예전엔 kill 한 옛 SoX 의 'close' 가 몇 ms 뒤에 와서 // 그사이 시작된 새 캡처('capturing')를 오류로 끝냈다(회의 중 마이크 테스트로 회의 마이크가 끊김). child.on('close', (code) => { if (this._soxProcess !== child) return + this._soxExited = true if (this._state === 'capturing') { logger.warn(`SoX process exited unexpectedly (code: ${code})`) this._handleError( @@ -238,6 +253,7 @@ class AudioCaptureService extends EventEmitter { child.on('error', (err: Error) => { if (this._soxProcess !== child) return + this._soxExited = true logger.error(`SoX process spawn error: ${err.message}`) const hint = soxExe === 'sox' && (err as NodeJS.ErrnoException).code === 'ENOENT' @@ -264,6 +280,7 @@ class AudioCaptureService extends EventEmitter { this.emit('started', { deviceId: this._currentDevice.deviceId }) logger.info('Audio capture started') } catch (error) { + this._cleanup() this._state = 'error' const d3roError = new D3ROError( ErrorCode.AudioCaptureStartFailed, @@ -626,11 +643,19 @@ class AudioCaptureService extends EventEmitter { this.emit('stopped', { reason: stopReason }) } + /** 현재 보유 중인 SoX 프로세스가 아직 살아 있는지 확인한다 */ + private _isSoxProcessAlive(): boolean { + const child = this._soxProcess + if (!child || this._soxExited) return false + return child.exitCode == null && !child.killed + } + /** * 녹음 프로세스와 타이머를 정리한다. */ private _cleanup(): void { this._stopLevelTimer() + this._soxExited = false if (this._stream) { this._stream.removeAllListeners() diff --git a/apps/desktop/src/main/services/CaptionService.ts b/apps/desktop/src/main/services/CaptionService.ts index c4ec7d1..489d0b2 100644 --- a/apps/desktop/src/main/services/CaptionService.ts +++ b/apps/desktop/src/main/services/CaptionService.ts @@ -128,6 +128,10 @@ class CaptionService extends EventEmitter { private _captionModelId: string | null = null private _refineQueue: CaptionSegment[] = [] private _refining = false + private _refineEpoch = 0 + private _refineRunId = 0 + private _refineController: AbortController | null = null + private _stopPromise: Promise | null = null /** 이번 세션이 잡은 오디오 소스(마이크 캡처·시스템 소리 요청) — 해제는 멱등 */ private _audio: CaptionAudioSources = this._createAudioSources() @@ -189,6 +193,9 @@ class CaptionService extends EventEmitter { if (epoch !== this._startEpoch || this._disposed) throw new CaptionStartCancelledError() } let ownAudio: CaptionAudioSources | null = null + let loadedAuxModelId: string | null = null + this._captionModelId = null + this._resetRefineState() this._setState('starting') // 오버레이를 즉시 표시 (로딩 상태) — 회의 모드처럼 자체 UI 가 있으면 띄우지 않는다 @@ -204,9 +211,13 @@ class CaptionService extends EventEmitter { await sttService.initialize() assertCurrent() // 자막 전용 모델을 골랐으면 받아쓰기 모델과 별도로 올려 둔다(보조 자리). - this._captionModelId = configGet('captionSttModelId') ?? null - if (this._captionModelId) await sttService.ensureAuxModel(this._captionModelId) + const candidateAuxModelId = configGet('captionSttModelId') ?? null + if (candidateAuxModelId) { + await sttService.ensureAuxModel(candidateAuxModelId) + loadedAuxModelId = candidateAuxModelId + } assertCurrent() + this._captionModelId = loadedAuxModelId // 세션 초기화 this._sessionId = crypto.randomUUID() @@ -261,7 +272,9 @@ class CaptionService extends EventEmitter { // 이 시도가 연 자원만 돌려준다(release 는 멱등). await ownAudio?.release() // stop() 이 내릴 때 아직 로딩 중이던 보조 모델이 뒤늦게 올라왔을 수 있다. 새 시작이 없을 때만 내린다. - if (this._captionModelId && this._state === 'inactive') void getLocalSTTService().releaseAuxModel() + if (loadedAuxModelId && (this._state === 'inactive' || this._state === 'stopping')) { + void getLocalSTTService().releaseAuxModel() + } logger.info('캡션 시작 취소: 로딩 중 정지됨') throw new D3ROError(ErrorCode.CaptionStartFailed, '캡션 시작이 취소되었습니다') } @@ -274,7 +287,9 @@ class CaptionService extends EventEmitter { await this._audio.release() this._hideOverlaySafely() // 시작 실패(오디오 장치 없음 등)에도 올려 둔 보조 모델을 내린다 — 안 그러면 다음 세션 종료까지 VRAM 을 잡는다. - if (this._captionModelId) void getLocalSTTService().releaseAuxModel() + const auxToRelease = this._captionModelId ?? loadedAuxModelId + this._captionModelId = null + if (auxToRelease) void getLocalSTTService().releaseAuxModel() this._session = { ...DEFAULT_START_OPTIONS } this._setState('inactive') const d3roErr = @@ -299,6 +314,13 @@ class CaptionService extends EventEmitter { * @returns 세션 요약(세그먼트가 없으면 null), 다른 소유자라 거부했으면 false */ async stop(owner?: CaptionOwner): Promise { + if (this._state === 'stopping' && this._stopPromise) { + if (owner && owner !== this._session.owner) { + logger.warn(`캡션 중지 거부: 세션 소유자=${this._session.owner}, 요청=${owner}`) + return false + } + return this._stopPromise + } if (this._state !== 'active' && this._state !== 'starting') { logger.warn(`캡션 중지 불가: 현재 상태=${this._state}`) return null @@ -311,7 +333,14 @@ class CaptionService extends EventEmitter { // 로딩 중(start 가 모델을 기다리는 중)이면 그 start 를 무효화한다 — 깨어나도 마이크를 열지 않는다. if (this._state === 'starting') this._startEpoch += 1 this._setState('stopping') + this._resetRefineState() + const stopRun = this._performStop() + this._stopPromise = stopRun + return stopRun + } + + private async _performStop(): Promise { // 타이머 정리 if (this._tickTimer) { clearInterval(this._tickTimer) @@ -335,7 +364,9 @@ class CaptionService extends EventEmitter { this._hideOverlaySafely() // 자막 전용 보조 모델은 세션이 끝나면 내린다 — 남겨 두면 받아쓰기 모델과 함께 VRAM 을 잡아 // Ollama 로드를 느리게 하거나 OOM 을 부른다. 다음 세션 시작 때 다시 올린다. - if (this._captionModelId) void getLocalSTTService().releaseAuxModel() + const auxToRelease = this._captionModelId + this._captionModelId = null + if (auxToRelease) void getLocalSTTService().releaseAuxModel() } // 세션을 히스토리에 저장 (회의 모드처럼 저장을 끈 소유자는 요약만 돌려받는다) @@ -352,6 +383,7 @@ class CaptionService extends EventEmitter { this._refineQueue = [] this._sessionLanguage = null this._session = { ...DEFAULT_START_OPTIONS } + this._stopPromise = null this._setState('inactive') getSoundEffectService().play('recording-stop') @@ -369,8 +401,9 @@ class CaptionService extends EventEmitter { async dispose(): Promise { if (this._disposed) return this._disposed = true + this._resetRefineState() - if (this._state === 'active' || this._state === 'starting') { + if (this._state === 'active' || this._state === 'starting' || this._state === 'stopping') { await this.stop() } @@ -522,7 +555,17 @@ class CaptionService extends EventEmitter { // ── 내부: 문맥 다듬기 ── + private _resetRefineState(): void { + this._refineEpoch += 1 + this._refineRunId += 1 + this._refineQueue = [] + this._refining = false + this._refineController?.abort() + this._refineController = null + } + private _enqueueRefine(segment: CaptionSegment): void { + if (this._disposed || this._state !== 'active') return if (configGet('captionRefineEnabled') === false) return if (!getLocalLLMService().isAvailable()) return this._refineQueue.push(segment) @@ -532,17 +575,30 @@ class CaptionService extends EventEmitter { } private async _drainRefineQueue(): Promise { + const runId = ++this._refineRunId + const epoch = this._refineEpoch this._refining = true try { - for (let next = this._refineQueue.shift(); next; next = this._refineQueue.shift()) { - await this._refine(next) + while ( + !this._disposed && + this._state === 'active' && + epoch === this._refineEpoch && + this._refineQueue.length > 0 + ) { + const next = this._refineQueue.shift() + if (!next) break + await this._refine(next, epoch) } } finally { - this._refining = false + if (this._refineRunId === runId) { + this._refining = false + this._refineController = null + } } } - private async _refine(segment: CaptionSegment): Promise { + private async _refine(segment: CaptionSegment, epoch: number): Promise { + if (this._disposed || this._state !== 'active' || epoch !== this._refineEpoch) return const index = this._segments.findIndex((item) => item.id === segment.id) if (index === -1) return const previous = this._segments.slice(Math.max(0, index - 2), index).map((item) => item.text) @@ -552,19 +608,27 @@ class CaptionService extends EventEmitter { previous, ...(glossary ? { glossary } : {}), }) + const controller = new AbortController() + this._refineController = controller try { const result = await getLocalLLMService().generate(text, { systemPrompt, temperature: 0, maxTokens: Math.min(256, segment.text.length * 2 + 32), timeoutMs: REFINE_TIMEOUT_MS, + signal: controller.signal, }) + if (this._disposed || this._state !== 'active' || epoch !== this._refineEpoch) return const refined = acceptCaptionRefinement(segment.text, result.text) if (!refined) return // 모델이 용어를 다른 표기로 되돌렸을 수 있다 — 정본 표기를 마지막에 다시 강제한다. this._replaceSegmentText(segment.id, getTranscriptGlossaryService().correct(refined).text) } catch (err) { logger.debug(`자막 다듬기 건너뜀: ${err instanceof Error ? err.message : String(err)}`) + } finally { + if (this._refineController === controller) { + this._refineController = null + } } } diff --git a/apps/desktop/src/main/services/LocalLLMService.ts b/apps/desktop/src/main/services/LocalLLMService.ts index ef0ce6d..53c02a8 100644 --- a/apps/desktop/src/main/services/LocalLLMService.ts +++ b/apps/desktop/src/main/services/LocalLLMService.ts @@ -186,6 +186,7 @@ class LocalLLMService extends EventEmitter { private _activeRequests = new Set() private _ensureRunningPromise: Promise<'running' | 'starting' | 'not-installed' | 'failed'> | null = null private _startCheckTimers = new Set>() + private _pullControllers = new Set() private _disposed = false get state(): LLMState { @@ -414,7 +415,7 @@ class LocalLLMService extends EventEmitter { * Ollama 가용성 폴링을 시작한다 (5초 간격). */ startPolling(): void { - if (this._pollInterval) return + if (this._pollInterval || this._disposed) return this._checkAvailability() this._pollInterval = setInterval(() => { if (this._state !== LLMState.Generating) { @@ -686,8 +687,13 @@ class LocalLLMService extends EventEmitter { text: string, action: LLMAction, targetLanguage?: string, - customPrompt?: string + customPrompt?: string, + options?: { signal?: AbortSignal; timeoutMs?: number } ): Promise { + if (options?.signal?.aborted) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LLM processing was cancelled') + } + // Phase 11: LLM 처리 쿼터 체크 try { const { getLicenseService } = await import('./LicenseService') @@ -708,10 +714,18 @@ class LocalLLMService extends EventEmitter { // LicenseService 미초기화 시 허용 } + if (options?.signal?.aborted) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LLM processing was cancelled') + } + const basePrompt = resolveSystemPrompt(action, targetLanguage, customPrompt) const systemPrompt = `${NO_THINK}\n${basePrompt}` - const result = await this.generate(text, { systemPrompt }) + const result = await this.generate(text, { + systemPrompt, + ...(options?.signal ? { signal: options.signal } : {}), + ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}) + }) const cleaned = stripReasoningBlocks(result.text) // reasoning 블록 제거 후 빈 응답이면 원본 텍스트 폴백 // (모델이 thinking만 하고 출력은 안 한 경우 / 응답 파싱 실패 케이스) @@ -784,67 +798,97 @@ class LocalLLMService extends EventEmitter { } private async _doPullModel(modelId: string): Promise { + if (this._disposed) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LocalLLMService disposed') + } + const serverUrl = getOllamaServerUrl() + const controller = new AbortController() + this._pullControllers.add(controller) + let reader: ReadableStreamDefaultReader | null = null + const onAbort = (): void => { + void reader?.cancel().catch(() => undefined) + } + controller.signal.addEventListener('abort', onAbort, { once: true }) logger.info(`Pull 시작: ${modelId}`) - const response = await fetch(`${serverUrl}/api/pull`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ model: modelId, stream: true }) - }) + try { + const response = await fetch(`${serverUrl}/api/pull`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: modelId, stream: true }), + signal: controller.signal + }) - if (!response.ok || !response.body) { - throw new D3ROError( - ErrorCode.LLMServerUnreachable, - `Pull 실패: HTTP ${response.status}` - ) - } + if (!response.ok || !response.body) { + throw new D3ROError( + ErrorCode.LLMServerUnreachable, + `Pull 실패: HTTP ${response.status}` + ) + } - const reader = response.body.getReader() - const decoder = new TextDecoder() - let buffer = '' + reader = response.body.getReader() + if (this._disposed || controller.signal.aborted) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`) + } + const decoder = new TextDecoder() + let buffer = '' - for (;;) { - const { done, value } = await reader.read() - if (done) break + for (;;) { + if (this._disposed || controller.signal.aborted) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`) + } + const { done, value } = await reader.read() + if (done) break - buffer += decoder.decode(value, { stream: true }) - const lines = buffer.split('\n') - buffer = lines.pop() ?? '' + buffer += decoder.decode(value, { stream: true }) + const lines = buffer.split('\n') + buffer = lines.pop() ?? '' - for (const line of lines) { - const trimmed = line.trim() - if (trimmed.length === 0) continue - try { - const chunk = JSON.parse(trimmed) as { - status: string - digest?: string - total?: number - completed?: number - error?: string + for (const line of lines) { + const trimmed = line.trim() + if (trimmed.length === 0) continue + try { + const chunk = JSON.parse(trimmed) as { + status: string + digest?: string + total?: number + completed?: number + error?: string + } + if (chunk.error) { + throw new D3ROError(ErrorCode.LLMServerUnreachable, chunk.error) + } + this.emit('pull-progress', { + modelId, + status: chunk.status, + digest: chunk.digest ?? null, + total: chunk.total ?? 0, + completed: chunk.completed ?? 0, + percent: + chunk.total && chunk.total > 0 + ? Math.min(100, Math.floor(((chunk.completed ?? 0) / chunk.total) * 100)) + : 0 + }) + } catch (err) { + if (err instanceof D3ROError) throw err + // JSON 파싱 실패한 부분 라인은 스킵 } - if (chunk.error) { - throw new D3ROError(ErrorCode.LLMServerUnreachable, chunk.error) - } - this.emit('pull-progress', { - modelId, - status: chunk.status, - digest: chunk.digest ?? null, - total: chunk.total ?? 0, - completed: chunk.completed ?? 0, - percent: - chunk.total && chunk.total > 0 - ? Math.min(100, Math.floor(((chunk.completed ?? 0) / chunk.total) * 100)) - : 0 - }) - } catch (err) { - if (err instanceof D3ROError) throw err - // JSON 파싱 실패한 부분 라인은 스킵 } } - } - logger.info(`Pull 완료: ${modelId}`) + if (this._disposed || controller.signal.aborted) { + throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`) + } + + logger.info(`Pull 완료: ${modelId}`) + } finally { + controller.signal.removeEventListener('abort', onAbort) + this._pullControllers.delete(controller) + if (reader) { + await reader.cancel().catch(() => undefined) + } + } } getStatus(): LLMStatus { @@ -989,8 +1033,14 @@ class LocalLLMService extends EventEmitter { clearTimeout(timer) } this._startCheckTimers.clear() + for (const controller of this._pullControllers) { + controller.abort() + } + this._pullControllers.clear() this.stopPolling() this.cancelGeneration() + this._available = false + this._serverVersion = null this._refreshState() this.removeAllListeners() logger.info('LocalLLMService disposed') @@ -1022,6 +1072,8 @@ class LocalLLMService extends EventEmitter { // ignore } + if (this._disposed) return + if (!isOk) { try { const tagsRes = await fetch(`${serverUrl}/api/tags`, { @@ -1035,6 +1087,8 @@ class LocalLLMService extends EventEmitter { } } + if (this._disposed) return + const wasAvailable = this._available this._available = isOk if (detectedVersion) this._serverVersion = detectedVersion diff --git a/apps/desktop/src/main/services/LocalSTTService.ts b/apps/desktop/src/main/services/LocalSTTService.ts index b4aa32e..1c26240 100644 --- a/apps/desktop/src/main/services/LocalSTTService.ts +++ b/apps/desktop/src/main/services/LocalSTTService.ts @@ -243,6 +243,8 @@ class LocalSTTService extends EventEmitter { // errorEmitted 플래그로 이벤트 중복 방지 private _errorEmitted: boolean = false private _idleUnloadTimer: NodeJS.Timeout | null = null + private _downloadControllers = new Set() + private _downloadSleepCancelers = new Set<() => void>() // ── 상태 접근자 ── @@ -521,13 +523,22 @@ class LocalSTTService extends EventEmitter { } await this._supervisor.ready() + if (this._disposed) { + throw new D3ROError( + ErrorCode.STTModelDownloadCancelled, + '모델 다운로드 중 서비스가 종료되었습니다', + ) + } - const startRes = await fetch(`${this._baseUrl}/download`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ model_id: modelId }), - signal: AbortSignal.timeout(10000), - }) + const startRes = await this._fetchDownload( + `${this._baseUrl}/download`, + { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model_id: modelId }), + }, + 10000, + ) // 409 = 이미 다운로드 진행 중 — 실패가 아니라 기존 진행에 합류해 폴링 if (!startRes.ok && startRes.status !== 409) { @@ -551,17 +562,33 @@ class LocalSTTService extends EventEmitter { // 진행률 폴링 — sidecar 통신 실패가 연속되면 중단 let consecutiveFailures = 0 for (;;) { + if (this._disposed) { + throw new D3ROError( + ErrorCode.STTModelDownloadCancelled, + `모델 다운로드 취소: ${modelId}`, + ) + } await this._sleep(500) + if (this._disposed) { + throw new D3ROError( + ErrorCode.STTModelDownloadCancelled, + `모델 다운로드 취소: ${modelId}`, + ) + } let status: DownloadStatusResponse try { - const res = await fetch(`${this._baseUrl}/download/status`, { - signal: AbortSignal.timeout(3000), - }) + const res = await this._fetchDownload(`${this._baseUrl}/download/status`, {}, 3000) if (!res.ok) throw new Error(`HTTP ${res.status}`) status = (await res.json()) as DownloadStatusResponse consecutiveFailures = 0 } catch (err) { + if (this._disposed) { + throw new D3ROError( + ErrorCode.STTModelDownloadCancelled, + `모델 다운로드 취소: ${modelId}`, + ) + } consecutiveFailures++ if (consecutiveFailures >= 5) { throw new D3ROError( @@ -572,6 +599,13 @@ class LocalSTTService extends EventEmitter { continue } + if (this._disposed) { + throw new D3ROError( + ErrorCode.STTModelDownloadCancelled, + `모델 다운로드 취소: ${modelId}`, + ) + } + this._emitDownloadProgress( modelId, status.percent, @@ -637,6 +671,14 @@ class LocalSTTService extends EventEmitter { logger.info('LocalSTTService dispose 시작') this._clearIdleUnload() + for (const cancelSleep of this._downloadSleepCancelers) { + cancelSleep() + } + this._downloadSleepCancelers.clear() + for (const controller of this._downloadControllers) { + controller.abort() + } + this._downloadControllers.clear() this._initializing = null this._modelReady = false this._auxModelId = null @@ -995,8 +1037,38 @@ class LocalSTTService extends EventEmitter { logger.error(`STT 에러: [${error.code}] ${error.message}`) } + private async _fetchDownload( + url: string, + init: RequestInit, + timeoutMs: number, + ): Promise { + if (this._disposed) { + throw new D3ROError(ErrorCode.STTModelDownloadCancelled, 'LocalSTTService가 dispose되었습니다') + } + const controller = new AbortController() + this._downloadControllers.add(controller) + const timer = setTimeout(() => controller.abort(), timeoutMs) + timer.unref?.() + try { + return await fetch(url, { ...init, signal: controller.signal }) + } finally { + clearTimeout(timer) + this._downloadControllers.delete(controller) + } + } + private _sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)) + if (this._disposed) return Promise.resolve() + return new Promise((resolve) => { + const cancel = (): void => { + clearTimeout(timer) + this._downloadSleepCancelers.delete(cancel) + resolve() + } + const timer = setTimeout(cancel, ms) + timer.unref?.() + this._downloadSleepCancelers.add(cancel) + }) } // ── 타입 안전한 이벤트 메서드 오버라이드 ── diff --git a/apps/desktop/src/main/services/VoiceConversationService.ts b/apps/desktop/src/main/services/VoiceConversationService.ts index c56c1dc..f4228f1 100644 --- a/apps/desktop/src/main/services/VoiceConversationService.ts +++ b/apps/desktop/src/main/services/VoiceConversationService.ts @@ -49,6 +49,7 @@ class VoiceConversationService extends EventEmitter { * 카운트가 쌓였고, 세션을 끝내도 마이크(SoX)가 계속 열려 있었다. */ private _micHeld = false + private _listenEpoch = 0 private _responseAbortController: AbortController | null = null /** 취소·세션 종료마다 올린다 — 전사가 끝났을 때 그 사이 취소됐는지 가린다(전사 중엔 abort 할 응답이 없다). */ private _turn = 0 @@ -122,8 +123,10 @@ class VoiceConversationService extends EventEmitter { this._turn += 1 this._stopListening() // 이 대화의 응답만 멈춘다 — signal 이 LLM 호출까지 전달된다(전역 cancelGeneration 은 다른 기능의 - // 진행 중 호출까지 끊었다). - this._responseAbortController?.abort() + // 진행 중 호출까지 끊었다). 포인터를 먼저 비워 취소 직후 새 턴이 막히지 않게 한다. + const responseController = this._responseAbortController + this._responseAbortController = null + responseController?.abort() getTTSPlaybackService().stop() this._isActive = false @@ -147,7 +150,9 @@ class VoiceConversationService extends EventEmitter { */ cancelResponse(): void { this._turn += 1 - this._responseAbortController?.abort() + const responseController = this._responseAbortController + this._responseAbortController = null + responseController?.abort() getTTSPlaybackService().stop() if (this._isActive && this._state !== 'listening') { this._resumeListening() @@ -266,7 +271,9 @@ class VoiceConversationService extends EventEmitter { if (!this._micHeld) { this._micHeld = true + const listenEpoch = ++this._listenEpoch audioService.start().catch((err) => { + if (!this._isActive || this._listenEpoch !== listenEpoch) return this._micHeld = false logger.error('Failed to start audio capture for conversation:', err) this._emitError('stt', 'Failed to start microphone') @@ -278,6 +285,7 @@ class VoiceConversationService extends EventEmitter { } private _stopListening(): void { + this._listenEpoch += 1 const audioService = getAudioCaptureService() if (this._audioListenerBound) { audioService.off('audio-data', this._onAudioData) diff --git a/apps/desktop/src/main/services/VoiceModeService.ts b/apps/desktop/src/main/services/VoiceModeService.ts index 19f0a47..a842e15 100644 --- a/apps/desktop/src/main/services/VoiceModeService.ts +++ b/apps/desktop/src/main/services/VoiceModeService.ts @@ -1023,8 +1023,10 @@ class VoiceModeService extends EventEmitter { action: LLMAction, targetLanguage?: string, customPrompt?: string, + signal?: AbortSignal, ): Promise { return getLlmGateway().processText(text, action, targetLanguage, customPrompt, { + ...(signal ? { signal } : {}), onFallback: (reason) => this._emitPremiumFallback(reason), }) } @@ -1087,8 +1089,8 @@ class VoiceModeService extends EventEmitter { const processedText = route.kind === 'instruction' - ? await this._processWithInstruction(userText, route.instructionId) - : await this._processWithAction(userText, route.action) + ? await this._processWithInstruction(userText, route.instructionId, run.controller.signal) + : await this._processWithAction(userText, route.action, run.controller.signal) if (!this._isCurrent(run)) return @@ -1115,7 +1117,11 @@ class VoiceModeService extends EventEmitter { } /** 지시문(음성 단축키 또는 활성 지시문·파이프라인)으로 처리한다. 지시문이 없으면 지시문 없이 custom 처리 */ - private async _processWithInstruction(userText: string, instructionId: string | null): Promise { + private async _processWithInstruction( + userText: string, + instructionId: string | null, + signal?: AbortSignal, + ): Promise { let invocationText = userText let instructionPrompt: string | undefined @@ -1146,13 +1152,14 @@ class VoiceModeService extends EventEmitter { } } - return this._runProcessorWithFallback(invocationText, 'custom', undefined, instructionPrompt) + return this._runProcessorWithFallback(invocationText, 'custom', undefined, instructionPrompt, signal) } /** 일반 액션(refine/translate/…)으로 처리한다 */ private async _processWithAction( userText: string, action: Extract['action'], + signal?: AbortSignal, ): Promise { const targetLanguage = resolveTargetLanguage() logger.info( @@ -1164,6 +1171,8 @@ class VoiceModeService extends EventEmitter { userText, action, action === 'translate' ? targetLanguage : undefined, + undefined, + signal, ) } diff --git a/apps/desktop/src/main/services/llm/LlmGateway.ts b/apps/desktop/src/main/services/llm/LlmGateway.ts index 51340a0..e3d2d7a 100644 --- a/apps/desktop/src/main/services/llm/LlmGateway.ts +++ b/apps/desktop/src/main/services/llm/LlmGateway.ts @@ -114,7 +114,13 @@ export interface PremiumLlmAdapter { export interface LocalLlmAdapter { isAvailable(): boolean - processText(text: string, action: LLMAction, targetLanguage?: string, customPrompt?: string): Promise + processText( + text: string, + action: LLMAction, + targetLanguage?: string, + customPrompt?: string, + options?: { signal?: AbortSignal; timeoutMs?: number }, + ): Promise generate( text: string, options?: LlmGenerateOptions & { signal?: AbortSignal; timeoutMs?: number }, @@ -214,8 +220,10 @@ class RoutedLlmGateway implements LlmGateway { } } throwIfAborted(hooks?.signal) - // 로컬 processText 는 호출 단위 signal 을 받지 않는다 — 끝난 뒤 취소됐으면 결과를 버린다. - const result = await this._deps.getLocal().processText(text, action, targetLanguage, customPrompt) + const perCall = callOptions(hooks) + const result = perCall + ? await this._deps.getLocal().processText(text, action, targetLanguage, customPrompt, perCall) + : await this._deps.getLocal().processText(text, action, targetLanguage, customPrompt) throwIfAborted(hooks?.signal) return result } diff --git a/apps/desktop/src/main/windows/WindowManager.ts b/apps/desktop/src/main/windows/WindowManager.ts index 40a5038..91d6a5e 100644 --- a/apps/desktop/src/main/windows/WindowManager.ts +++ b/apps/desktop/src/main/windows/WindowManager.ts @@ -567,6 +567,8 @@ ipcMain.on(IPC_CHANNELS.POPUP_RESULT.MEASURED, (event: Electron.IpcMainEvent, da }) export function hideResultPopup(): void { + cancelPendingResultShow() + lastResultCursor = null if (resultPopupWindow && !resultPopupWindow.isDestroyed()) { resultPopupWindow.hide() } diff --git a/apps/desktop/tests/main/ipc/ipc-redteam-r3-1.test.ts b/apps/desktop/tests/main/ipc/ipc-redteam-r3-1.test.ts index 68679b5..73f2f6d 100644 --- a/apps/desktop/tests/main/ipc/ipc-redteam-r3-1.test.ts +++ b/apps/desktop/tests/main/ipc/ipc-redteam-r3-1.test.ts @@ -6,6 +6,7 @@ // - realtime-token 은 주입된 자격 증명 포트로 호출한다 (item 2) import { beforeEach, describe, expect, it, vi } from 'vitest' +import { desktopCapturer, session } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' const handlers = vi.hoisted(() => new Map Promise>()) @@ -136,3 +137,25 @@ describe('realtime-token 은 자격 증명 포트를 쓴다', () => { expect(invokeFn).toHaveBeenCalledWith('realtime-token', {}) }) }) + +describe('systemAudio:enableLoopback 예외 처리', () => { + it('enableLoopback_getSourcesRejects_invokesCallbackWithEmptyObject', async () => { + const mod = await import('../../../src/main/ipc/caption-handlers') + mod.registerCaptionHandlers() + + const regResult = await invoke(IPC_CHANNELS.SYSTEM_AUDIO.ENABLE_LOOPBACK) + expect(regResult.success).toBe(true) + + const setHandlerCalls = vi.mocked(session.defaultSession.setDisplayMediaRequestHandler).mock.calls + const mediaHandler = setHandlerCalls[0]?.[0] as + | ((request: unknown, callback: (streams: Record) => void) => Promise) + | undefined + expect(typeof mediaHandler).toBe('function') + + vi.mocked(desktopCapturer.getSources).mockRejectedValueOnce(new Error('screen capture failed')) + const callback = vi.fn() + + await expect(mediaHandler!({}, callback)).resolves.toBeUndefined() + expect(callback).toHaveBeenCalledWith({}) + }) +}) diff --git a/apps/desktop/tests/main/services/VoiceConversationService.test.ts b/apps/desktop/tests/main/services/VoiceConversationService.test.ts index 2d587c4..dcae2de 100644 --- a/apps/desktop/tests/main/services/VoiceConversationService.test.ts +++ b/apps/desktop/tests/main/services/VoiceConversationService.test.ts @@ -190,4 +190,64 @@ describe('VoiceConversationService local response ownership', () => { expect(mocks.speakSentences).not.toHaveBeenCalled() expect(service.getHistory().map((message) => message.content)).toEqual(['cancel before response']) }) + + it('cancelResponse_followedImmediatelyBySendTextMessage_startsNewTurnWithoutConflict', async () => { + let releaseFirstStream: (() => void) | undefined + mocks.chatStream + .mockImplementationOnce(() => (async function* (): AsyncGenerator { + await new Promise((resolve) => { + releaseFirstStream = resolve + }) + return '' + })()) + .mockImplementationOnce(() => (async function* (): AsyncGenerator { + yield 'second reply.' + return 'second reply.' + })()) + + const service = getVoiceConversationService() + await service.startSession() + + const firstResponse = service.sendTextMessage('first question') + await vi.waitFor(() => expect(mocks.chatStream).toHaveBeenCalledTimes(1)) + + service.cancelResponse() + const secondResponse = service.sendTextMessage('second question') + releaseFirstStream?.() + + await expect(secondResponse).resolves.toBeUndefined() + await firstResponse + expect(mocks.chatStream).toHaveBeenCalledTimes(2) + expect(service.getHistory().map((m) => m.content)).toEqual([ + 'first question', + 'second question', + 'second reply.', + ]) + }) + + it('startSession_audioStartRejectsAfterStop_doesNotEmitSttErrorOrClobberNewSessionMic', async () => { + let rejectFirstMic!: (err: Error) => void + mocks.audioStart + .mockImplementationOnce(() => new Promise((_resolve, reject) => { + rejectFirstMic = reject + })) + .mockImplementationOnce(async () => undefined) + + const service = getVoiceConversationService() + const errorEvents: Array<{ phase: string; message: string }> = [] + service.on('error', (payload) => errorEvents.push(payload)) + + await service.startSession() + service.stopSession() + + await service.startSession() + rejectFirstMic(new Error('stale mic failure')) + await new Promise((r) => setTimeout(r, 0)) + + expect(errorEvents).toEqual([]) + expect(mocks.send.mock.calls.filter(([channel]) => channel === IPC_CHANNELS.VOICE_CONVERSATION.ERROR)).toEqual([]) + + service.stopSession() + expect(mocks.audioStop).toHaveBeenCalledTimes(2) + }) }) diff --git a/apps/desktop/tests/main/services/VoiceModeService.test.ts b/apps/desktop/tests/main/services/VoiceModeService.test.ts index a5775cc..f3bf76d 100644 --- a/apps/desktop/tests/main/services/VoiceModeService.test.ts +++ b/apps/desktop/tests/main/services/VoiceModeService.test.ts @@ -261,6 +261,36 @@ describe('VoiceModeService', () => { expect(cancelReason).toBe('user') }) + + it('cancelSession_whileLlmProcessingInFlight_abortsPerCallSignalPassedToLlm', async () => { + let capturedSignal: AbortSignal | undefined + let rejectLlm: ((reason?: unknown) => void) | undefined + mockLLM.processText.mockImplementation(((_text, _action, _lang, _prompt, options?: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return new Promise((_resolve, reject) => { + rejectLlm = reject + options?.signal?.addEventListener('abort', () => reject(new Error('llm aborted')), { once: true }) + }) + }) as never) + + const svc = getVoiceModeService() + await svc.startSession('dictation') + audioBus.emit('audio-data', { buffer: Buffer.alloc(16000 * 2) }) + await new Promise((r) => setTimeout(r, 850)) + const stopping = svc.stopSession() + + await vi.waitFor(() => expect(mockLLM.processText).toHaveBeenCalledTimes(1)) + + expect(capturedSignal).toBeInstanceOf(AbortSignal) + expect(capturedSignal?.aborted).toBe(false) + + svc.cancelSession() + rejectLlm?.(new Error('cleanup')) + await stopping + + expect(capturedSignal?.aborted).toBe(true) + expect(mockTextInsert.insertText).not.toHaveBeenCalled() + }) }) describe('LLM 후처리 인자 전달', () => { diff --git a/apps/desktop/tests/main/services/audio-capture-redteam-r2-7.test.ts b/apps/desktop/tests/main/services/audio-capture-redteam-r2-7.test.ts index c096513..419f484 100644 --- a/apps/desktop/tests/main/services/audio-capture-redteam-r2-7.test.ts +++ b/apps/desktop/tests/main/services/audio-capture-redteam-r2-7.test.ts @@ -7,6 +7,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { EventEmitter } from 'events' +import { spawn } from 'child_process' import { ipcMain } from 'electron' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' @@ -228,3 +229,37 @@ describe('마이크 테스트 IPC', () => { expect(proc(0).kill).toHaveBeenCalledTimes(1) }) }) + +describe('정지/시작 레이스 및 실패 정리', () => { + it('start_calledWhileStoppingAfterSoxClosed_spawnsFreshSoxProcess', async () => { + const audio = mod.getAudioCaptureService() + await audio.start() + + const stopping = audio.stop() + proc(0).emit('close', 1) + + await audio.start() + await stopping + + expect(spawned.list).toHaveLength(2) + expect(audio.state).toBe('capturing') + + await audio.stop() + expect(proc(1).kill).toHaveBeenCalledTimes(1) + }) + + it('start_spawnedProcessMissingStdout_cleansUpChildProcess', async () => { + const brokenChild = Object.assign(new EventEmitter(), { + stdout: null, + stderr: new EventEmitter(), + kill: vi.fn(), + }) + vi.mocked(spawn).mockReturnValueOnce(brokenChild as never) + + const audio = mod.getAudioCaptureService() + await expect(audio.start()).rejects.toMatchObject({ message: expect.stringMatching(/stdout/i) }) + + expect(audio.state).toBe('error') + expect(brokenChild.kill).toHaveBeenCalledTimes(1) + }) +}) diff --git a/apps/desktop/tests/main/services/caption-aux-release.test.ts b/apps/desktop/tests/main/services/caption-aux-release.test.ts index 1bfdcee..8e392fe 100644 --- a/apps/desktop/tests/main/services/caption-aux-release.test.ts +++ b/apps/desktop/tests/main/services/caption-aux-release.test.ts @@ -16,11 +16,12 @@ vi.mock('../../../src/main/services/ConfigService', () => ({ const stt = vi.hoisted(() => ({ gate: null as Promise | 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 | 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((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() + }) }) diff --git a/apps/desktop/tests/main/services/caption-lifecycle-refine.test.ts b/apps/desktop/tests/main/services/caption-lifecycle-refine.test.ts new file mode 100644 index 0000000..2dda7c6 --- /dev/null +++ b/apps/desktop/tests/main/services/caption-lifecycle-refine.test.ts @@ -0,0 +1,178 @@ +// tests/main/services/caption-lifecycle-refine.test.ts +// CaptionService concurrent stop & refine queue cancellation/isolation regression tests. + +import { beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('../../../src/main/services/LoggerService', () => ({ + getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }), +})) + +const config = vi.hoisted(() => ({ values: {} as Record })) +vi.mock('../../../src/main/services/ConfigService', () => ({ + configGet: vi.fn((key: string) => config.values[key]), + configSet: vi.fn(), +})) + +vi.mock('../../../src/main/services/LocalSTTService', () => ({ + getLocalSTTService: () => ({ + initialize: vi.fn(async () => undefined), + ensureAuxModel: vi.fn(async () => undefined), + releaseAuxModel: vi.fn(async () => undefined), + transcribe: vi.fn(async () => ({ text: '', segments: [] })), + }), +})) + +const audio = vi.hoisted(() => ({ + stopGate: null as Promise | 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, +})) + +vi.mock('../../../src/main/services/SoundEffectService', () => ({ + getSoundEffectService: () => ({ play: vi.fn() }), +})) + +const llm = vi.hoisted(() => ({ + available: true, + generate: vi.fn< + [string, { signal?: AbortSignal; systemPrompt?: string }], + Promise<{ text: string }> + >(async (_prompt, _opts) => ({ text: '' })), +})) +vi.mock('../../../src/main/services/LocalLLMService', () => ({ + getLocalLLMService: () => ({ + isAvailable: () => llm.available, + generate: llm.generate, + }), +})) + +vi.mock('../../../src/main/services/HistoryService', () => ({ + getHistoryService: () => ({ create: vi.fn(() => ({ id: 'history-1' })) }), +})) + +vi.mock('../../../src/main/services/TranscriptGlossaryService', () => ({ + getTranscriptGlossaryService: () => ({ + correct: (text: string) => ({ text, corrections: [] }), + promptSection: () => '', + }), +})) + +const tracks = vi.hoisted(() => ({ + latestDeps: null as { onFinal: (text: string) => void } | null, + flushGate: null as Promise | null, +})) +vi.mock('../../../src/main/services/caption/StreamingCaptionTrack', () => ({ + StreamingCaptionTrack: class { + constructor(private readonly deps: { onFinal: (text: string) => void }) { + tracks.latestDeps = deps + } + push(): void {} + async tick(): Promise {} + async flush(): Promise { + if (tracks.flushGate) await tracks.flushGate + } + }, +})) + +type CaptionModule = typeof import('../../../src/main/services/CaptionService') +let mod: CaptionModule + +beforeEach(async () => { + vi.resetModules() + vi.clearAllMocks() + config.values = { captionRefineEnabled: true } + audio.stopGate = null + tracks.latestDeps = null + tracks.flushGate = null + llm.available = true + llm.generate.mockReset() + llm.generate.mockImplementation(async () => ({ text: '' })) + mod = await import('../../../src/main/services/CaptionService') + mod.resetCaptionServiceForTests() +}) + +describe('CaptionService concurrent stop and refine lifecycle', () => { + it('stop_calledConcurrentlyWhileStopping_waitsForTeardownBeforeResolving', async () => { + let releaseAudio!: () => void + audio.stopGate = new Promise((r) => { + releaseAudio = r + }) + + const caption = mod.getCaptionService() + await caption.start({ audioSource: 'mic', showOverlay: false, persistHistory: false }) + tracks.latestDeps?.onFinal('첫 번째 확정 문장') + + const firstStop = caption.stop() + await new Promise((r) => setTimeout(r, 0)) + expect(caption.getState()).toBe('stopping') + + let secondResolved = false + const secondStop = caption.stop().then((result) => { + secondResolved = true + return result + }) + await new Promise((r) => setTimeout(r, 0)) + + // Second stop must not resolve null prematurely before audio release finishes + expect(secondResolved).toBe(false) + + releaseAudio() + const [r1, r2] = await Promise.all([firstStop, secondStop]) + expect(caption.getState()).toBe('inactive') + expect(r1 && typeof r1 === 'object' ? r1.segments.map((s) => s.text) : []).toEqual([ + '첫 번째 확정 문장', + ]) + expect(r2).toEqual(r1) + }) + + it('refine_stoppedWhileLlmInFlight_abortsGenerateAndAllowsNextSessionRefine', async () => { + const signals: Array = [] + let rejectFirstOnAbort: (() => void) | null = null + let callCount = 0 + + llm.generate.mockImplementation(async (_text, opts) => { + callCount += 1 + signals.push(opts?.signal) + if (callCount === 1) { + return await new Promise<{ text: string }>((_, reject) => { + rejectFirstOnAbort = () => reject(new Error('aborted')) + }) + } + return { text: '오늘 회의는 세 시에 시작합니다.' } + }) + + const caption = mod.getCaptionService() + const updated: Array<{ id: string; text: string }> = [] + caption.on('segment-updated', (u) => updated.push(u)) + + await caption.start({ audioSource: 'mic', showOverlay: false, persistHistory: false }) + tracks.latestDeps?.onFinal('오늘회의는 세시에 시작합니다') + await new Promise((r) => setTimeout(r, 0)) + + expect(llm.generate).toHaveBeenCalledTimes(1) + expect(signals[0]).toBeInstanceOf(AbortSignal) + expect(signals[0]?.aborted).toBe(false) + + await caption.stop() + expect(signals[0]?.aborted).toBe(true) + + // Start a brand-new session BEFORE the aborted first LLM promise settles + await caption.start({ audioSource: 'mic', showOverlay: false, persistHistory: false }) + tracks.latestDeps?.onFinal('오늘회의는 세시에 시작합니다') + await new Promise((r) => setTimeout(r, 0)) + + expect(llm.generate).toHaveBeenCalledTimes(2) + expect(updated.length).toBe(1) + + // Settling the aborted request later must not emit or break state + rejectFirstOnAbort?.() + await new Promise((r) => setTimeout(r, 0)) + await caption.stop() + }) +}) diff --git a/apps/desktop/tests/main/services/llm-limits-routing-redteam-r2-1.test.ts b/apps/desktop/tests/main/services/llm-limits-routing-redteam-r2-1.test.ts index f86ab5c..b667871 100644 --- a/apps/desktop/tests/main/services/llm-limits-routing-redteam-r2-1.test.ts +++ b/apps/desktop/tests/main/services/llm-limits-routing-redteam-r2-1.test.ts @@ -151,4 +151,17 @@ describe('LlmGateway 호출 단위 취소', () => { }) expect(l.processText).not.toHaveBeenCalled() }) + + it('processText_localBackendWithSignal_forwardsPerCallSignalToLocalAdapter', async () => { + const controller = new AbortController() + const l = local() + const gw = createRoutedLlmGateway({ + getBackend: () => 'local', + loadPremium: async () => premium(), + getLocal: () => l, + }) + + await gw.processText('t', 'refine', undefined, undefined, { signal: controller.signal }) + expect(l.processText).toHaveBeenCalledWith('t', 'refine', undefined, undefined, { signal: controller.signal }) + }) }) diff --git a/apps/desktop/tests/main/services/local-llm-service.test.ts b/apps/desktop/tests/main/services/local-llm-service.test.ts index 881ae92..0470649 100644 --- a/apps/desktop/tests/main/services/local-llm-service.test.ts +++ b/apps/desktop/tests/main/services/local-llm-service.test.ts @@ -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((_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 + } + ).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 | 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({ + pull() { + return new Promise((_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((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) + }) +}) diff --git a/apps/desktop/tests/main/services/stt-sidecar-redteam-r2-6.test.ts b/apps/desktop/tests/main/services/stt-sidecar-redteam-r2-6.test.ts index 5aa5a14..088c968 100644 --- a/apps/desktop/tests/main/services/stt-sidecar-redteam-r2-6.test.ts +++ b/apps/desktop/tests/main/services/stt-sidecar-redteam-r2-6.test.ts @@ -374,4 +374,54 @@ describe('LocalSTTService — 사이드카 크래시 재시작', () => { expect(local.currentModelId).toBe('tiny') await local.dispose() }) + + it('downloadModel_disposedWhilePolling_abortsPromptlyWithoutPollingAfterDispose', async () => { + const h = createHarness() + h.setHealthy(true) + let statusPollsAfterDispose = 0 + let disposed = false + + const wrappedFetch = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = String(input) + if (url.endsWith('/download')) { + return jsonResponse({ status: 'downloading' }) + } + if (url.endsWith('/download/status')) { + if (disposed) { + statusPollsAfterDispose++ + if (statusPollsAfterDispose >= 2) { + return jsonResponse({ + status: 'done', + percent: 100, + downloaded_bytes: 100, + total_bytes: 100, + bytes_per_second: 100, + }) + } + } + return jsonResponse({ + status: 'downloading', + percent: 10, + downloaded_bytes: 10, + total_bytes: 100, + bytes_per_second: 10, + }) + } + 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), + ) + + disposed = true + const disposePromise = local.dispose() + + await expect(downloadPromise).rejects.toMatchObject({ code: ErrorCode.STTModelDownloadCancelled }) + await disposePromise + expect(statusPollsAfterDispose).toBe(0) + }) }) diff --git a/apps/desktop/tests/main/windows/popup-hide-show-race.test.ts b/apps/desktop/tests/main/windows/popup-hide-show-race.test.ts index 508bb73..1984e56 100644 --- a/apps/desktop/tests/main/windows/popup-hide-show-race.test.ts +++ b/apps/desktop/tests/main/windows/popup-hide-show-race.test.ts @@ -139,4 +139,23 @@ describe('WindowManager popup hide/show timer race regressions', () => { await vi.advanceTimersByTimeAsync(160) expect(wm.isCommandPopupVisible()).toBe(false) }) + + it('hideResultPopup_calledBeforeMeasureTimeout_cancelsPendingShowAndStaysHidden', async () => { + const wm = await import('../../../src/main/windows/WindowManager') + + wm.showResultPopup('hello world', 5000) + const popupWin = state.windows[0] + expect(popupWin?.visible).toBe(true) + + // Hide before RESULT_MEASURE_TIMEOUT_MS (400ms) fires + await vi.advanceTimersByTimeAsync(100) + wm.hideResultPopup() + expect(popupWin?.visible).toBe(false) + + const sendsBeforeTimeout = popupWin?.webContents.send.mock.calls.length ?? 0 + await vi.advanceTimersByTimeAsync(500) + + expect(popupWin?.visible).toBe(false) + expect(popupWin?.webContents.send.mock.calls.length ?? 0).toBe(sendsBeforeTimeout) + }) })