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

@ -5,6 +5,7 @@ import { ipcMain, session, desktopCapturer } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors' import { ipcSuccess, ipcError, ErrorCode } from '@d3ro/core/errors'
import { getCaptionService } from '../services/CaptionService' import { getCaptionService } from '../services/CaptionService'
import { getLogger } from '../services/LoggerService'
import type { CaptionConfig } from '@d3ro/core/types' import type { CaptionConfig } from '@d3ro/core/types'
import { import {
endCaptionOverlayDrag, endCaptionOverlayDrag,
@ -13,6 +14,8 @@ import {
startCaptionOverlayDrag startCaptionOverlayDrag
} from '../windows/WindowManager' } from '../windows/WindowManager'
const logger = getLogger('CaptionHandlers')
export function registerCaptionHandlers(): void { export function registerCaptionHandlers(): void {
// ── 자막 창 손잡이 (팝업 → 메인) ── // ── 자막 창 손잡이 (팝업 → 메인) ──
ipcMain.on(IPC_CHANNELS.POPUP_CAPTION.SET_INTERACTIVE, (_event, interactive: unknown) => { ipcMain.on(IPC_CHANNELS.POPUP_CAPTION.SET_INTERACTIVE, (_event, interactive: unknown) => {
@ -25,12 +28,19 @@ export function registerCaptionHandlers(): void {
// 시스템 오디오 루프백: setDisplayMediaRequestHandler로 audio: 'loopback' 설정 // 시스템 오디오 루프백: setDisplayMediaRequestHandler로 audio: 'loopback' 설정
ipcMain.handle(IPC_CHANNELS.SYSTEM_AUDIO.ENABLE_LOOPBACK, async () => { ipcMain.handle(IPC_CHANNELS.SYSTEM_AUDIO.ENABLE_LOOPBACK, async () => {
session.defaultSession.setDisplayMediaRequestHandler(async (_request, callback) => { session.defaultSession.setDisplayMediaRequestHandler(async (_request, callback) => {
const sources = await desktopCapturer.getSources({ types: ['screen'] }) try {
if (sources.length === 0) { 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({}) callback({})
return
} }
callback({ video: sources[0], audio: 'loopback' })
}) })
return ipcSuccess(undefined) return ipcSuccess(undefined)
}) })

View file

@ -94,6 +94,7 @@ class AudioCaptureService extends EventEmitter {
private _refCount = 0 private _refCount = 0
private _cachedDevices: AudioDevice[] | null = null private _cachedDevices: AudioDevice[] | null = null
private _soxProcess: ChildProcess | null = null private _soxProcess: ChildProcess | null = null
private _soxExited = false
private _stream: Readable | null = null private _stream: Readable | null = null
private _levelInterval: ReturnType<typeof setInterval> | null = null private _levelInterval: ReturnType<typeof setInterval> | null = null
/** 새 SoX 를 띄울 때마다 증가 — 임대(lease)가 자기 캡처인지 판단한다 */ /** 새 SoX 를 띄울 때마다 증가 — 임대(lease)가 자기 캡처인지 판단한다 */
@ -155,15 +156,21 @@ class AudioCaptureService extends EventEmitter {
return return
} }
// 마지막 참조가 꼬리를 받는 중에 새 소비자가 오면 정지를 취소하고 같은 SoX 로 이어 간다 // 마지막 참조가 꼬리를 받는 중에 새 소비자가 오면 SoX가 아직 살아 있을 때만 정지를 취소하고 이어 간다
if (this._state === 'stopping' && this._pendingStopToken !== 0) { if (this._state === 'stopping' && this._pendingStopToken !== 0) {
this._pendingStopToken = 0 this._pendingStopToken = 0
this._cancelTailWait?.() this._cancelTailWait?.()
this._state = 'capturing' if (this._isSoxProcessAlive()) {
this._refCount = 1 this._state = 'capturing'
this._startLevelTimer() this._refCount = 1
logger.debug('Pending stop cancelled by a new consumer — capture continues') this._startLevelTimer()
return 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') { if (this._state !== 'idle' && this._state !== 'error') {
@ -202,6 +209,7 @@ class AudioCaptureService extends EventEmitter {
logger.info(`SoX args: ${soxArgs.join(' ')}`) logger.info(`SoX args: ${soxArgs.join(' ')}`)
const child = spawn(soxExe, soxArgs, { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }) const child = spawn(soxExe, soxArgs, { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true })
this._soxProcess = child this._soxProcess = child
this._soxExited = false
this._generation++ this._generation++
this._stream = child.stdout this._stream = child.stdout
this._residualBuffer = Buffer.alloc(0) this._residualBuffer = Buffer.alloc(0)
@ -222,11 +230,18 @@ class AudioCaptureService extends EventEmitter {
if (msg) logger.warn(`SoX stderr: ${msg}`) if (msg) logger.warn(`SoX stderr: ${msg}`)
}) })
child.once('exit', () => {
if (this._soxProcess === child) {
this._soxExited = true
}
})
// SoX 프로세스 종료 감지. // SoX 프로세스 종료 감지.
// 자기 프로세스인지 먼저 확인한다 — 예전엔 kill 한 옛 SoX 의 'close' 가 몇 ms 뒤에 와서 // 자기 프로세스인지 먼저 확인한다 — 예전엔 kill 한 옛 SoX 의 'close' 가 몇 ms 뒤에 와서
// 그사이 시작된 새 캡처('capturing')를 오류로 끝냈다(회의 중 마이크 테스트로 회의 마이크가 끊김). // 그사이 시작된 새 캡처('capturing')를 오류로 끝냈다(회의 중 마이크 테스트로 회의 마이크가 끊김).
child.on('close', (code) => { child.on('close', (code) => {
if (this._soxProcess !== child) return if (this._soxProcess !== child) return
this._soxExited = true
if (this._state === 'capturing') { if (this._state === 'capturing') {
logger.warn(`SoX process exited unexpectedly (code: ${code})`) logger.warn(`SoX process exited unexpectedly (code: ${code})`)
this._handleError( this._handleError(
@ -238,6 +253,7 @@ class AudioCaptureService extends EventEmitter {
child.on('error', (err: Error) => { child.on('error', (err: Error) => {
if (this._soxProcess !== child) return if (this._soxProcess !== child) return
this._soxExited = true
logger.error(`SoX process spawn error: ${err.message}`) logger.error(`SoX process spawn error: ${err.message}`)
const hint = const hint =
soxExe === 'sox' && (err as NodeJS.ErrnoException).code === 'ENOENT' soxExe === 'sox' && (err as NodeJS.ErrnoException).code === 'ENOENT'
@ -264,6 +280,7 @@ class AudioCaptureService extends EventEmitter {
this.emit('started', { deviceId: this._currentDevice.deviceId }) this.emit('started', { deviceId: this._currentDevice.deviceId })
logger.info('Audio capture started') logger.info('Audio capture started')
} catch (error) { } catch (error) {
this._cleanup()
this._state = 'error' this._state = 'error'
const d3roError = new D3ROError( const d3roError = new D3ROError(
ErrorCode.AudioCaptureStartFailed, ErrorCode.AudioCaptureStartFailed,
@ -626,11 +643,19 @@ class AudioCaptureService extends EventEmitter {
this.emit('stopped', { reason: stopReason }) 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 { private _cleanup(): void {
this._stopLevelTimer() this._stopLevelTimer()
this._soxExited = false
if (this._stream) { if (this._stream) {
this._stream.removeAllListeners() this._stream.removeAllListeners()

View file

@ -128,6 +128,10 @@ class CaptionService extends EventEmitter {
private _captionModelId: string | null = null private _captionModelId: string | null = null
private _refineQueue: CaptionSegment[] = [] private _refineQueue: CaptionSegment[] = []
private _refining = false private _refining = false
private _refineEpoch = 0
private _refineRunId = 0
private _refineController: AbortController | null = null
private _stopPromise: Promise<CaptionSessionResult | null> | null = null
/** 이번 세션이 잡은 오디오 소스(마이크 캡처·시스템 소리 요청) — 해제는 멱등 */ /** 이번 세션이 잡은 오디오 소스(마이크 캡처·시스템 소리 요청) — 해제는 멱등 */
private _audio: CaptionAudioSources = this._createAudioSources() private _audio: CaptionAudioSources = this._createAudioSources()
@ -189,6 +193,9 @@ class CaptionService extends EventEmitter {
if (epoch !== this._startEpoch || this._disposed) throw new CaptionStartCancelledError() if (epoch !== this._startEpoch || this._disposed) throw new CaptionStartCancelledError()
} }
let ownAudio: CaptionAudioSources | null = null let ownAudio: CaptionAudioSources | null = null
let loadedAuxModelId: string | null = null
this._captionModelId = null
this._resetRefineState()
this._setState('starting') this._setState('starting')
// 오버레이를 즉시 표시 (로딩 상태) — 회의 모드처럼 자체 UI 가 있으면 띄우지 않는다 // 오버레이를 즉시 표시 (로딩 상태) — 회의 모드처럼 자체 UI 가 있으면 띄우지 않는다
@ -204,9 +211,13 @@ class CaptionService extends EventEmitter {
await sttService.initialize() await sttService.initialize()
assertCurrent() assertCurrent()
// 자막 전용 모델을 골랐으면 받아쓰기 모델과 별도로 올려 둔다(보조 자리). // 자막 전용 모델을 골랐으면 받아쓰기 모델과 별도로 올려 둔다(보조 자리).
this._captionModelId = configGet('captionSttModelId') ?? null const candidateAuxModelId = configGet('captionSttModelId') ?? null
if (this._captionModelId) await sttService.ensureAuxModel(this._captionModelId) if (candidateAuxModelId) {
await sttService.ensureAuxModel(candidateAuxModelId)
loadedAuxModelId = candidateAuxModelId
}
assertCurrent() assertCurrent()
this._captionModelId = loadedAuxModelId
// 세션 초기화 // 세션 초기화
this._sessionId = crypto.randomUUID() this._sessionId = crypto.randomUUID()
@ -261,7 +272,9 @@ class CaptionService extends EventEmitter {
// 이 시도가 연 자원만 돌려준다(release 는 멱등). // 이 시도가 연 자원만 돌려준다(release 는 멱등).
await ownAudio?.release() await ownAudio?.release()
// stop() 이 내릴 때 아직 로딩 중이던 보조 모델이 뒤늦게 올라왔을 수 있다. 새 시작이 없을 때만 내린다. // stop() 이 내릴 때 아직 로딩 중이던 보조 모델이 뒤늦게 올라왔을 수 있다. 새 시작이 없을 때만 내린다.
if (this._captionModelId && this._state === 'inactive') void getLocalSTTService().releaseAuxModel() if (loadedAuxModelId && (this._state === 'inactive' || this._state === 'stopping')) {
void getLocalSTTService().releaseAuxModel()
}
logger.info('캡션 시작 취소: 로딩 중 정지됨') logger.info('캡션 시작 취소: 로딩 중 정지됨')
throw new D3ROError(ErrorCode.CaptionStartFailed, '캡션 시작이 취소되었습니다') throw new D3ROError(ErrorCode.CaptionStartFailed, '캡션 시작이 취소되었습니다')
} }
@ -274,7 +287,9 @@ class CaptionService extends EventEmitter {
await this._audio.release() await this._audio.release()
this._hideOverlaySafely() this._hideOverlaySafely()
// 시작 실패(오디오 장치 없음 등)에도 올려 둔 보조 모델을 내린다 — 안 그러면 다음 세션 종료까지 VRAM 을 잡는다. // 시작 실패(오디오 장치 없음 등)에도 올려 둔 보조 모델을 내린다 — 안 그러면 다음 세션 종료까지 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._session = { ...DEFAULT_START_OPTIONS }
this._setState('inactive') this._setState('inactive')
const d3roErr = const d3roErr =
@ -299,6 +314,13 @@ class CaptionService extends EventEmitter {
* @returns 세션 요약(세그먼트가 없으면 null), 다른 소유자라 거부했으면 false * @returns 세션 요약(세그먼트가 없으면 null), 다른 소유자라 거부했으면 false
*/ */
async stop(owner?: CaptionOwner): Promise<CaptionSessionResult | null | false> { async stop(owner?: CaptionOwner): Promise<CaptionSessionResult | null | false> {
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') { if (this._state !== 'active' && this._state !== 'starting') {
logger.warn(`캡션 중지 불가: 현재 상태=${this._state}`) logger.warn(`캡션 중지 불가: 현재 상태=${this._state}`)
return null return null
@ -311,7 +333,14 @@ class CaptionService extends EventEmitter {
// 로딩 중(start 가 모델을 기다리는 중)이면 그 start 를 무효화한다 — 깨어나도 마이크를 열지 않는다. // 로딩 중(start 가 모델을 기다리는 중)이면 그 start 를 무효화한다 — 깨어나도 마이크를 열지 않는다.
if (this._state === 'starting') this._startEpoch += 1 if (this._state === 'starting') this._startEpoch += 1
this._setState('stopping') this._setState('stopping')
this._resetRefineState()
const stopRun = this._performStop()
this._stopPromise = stopRun
return stopRun
}
private async _performStop(): Promise<CaptionSessionResult | null> {
// 타이머 정리 // 타이머 정리
if (this._tickTimer) { if (this._tickTimer) {
clearInterval(this._tickTimer) clearInterval(this._tickTimer)
@ -335,7 +364,9 @@ class CaptionService extends EventEmitter {
this._hideOverlaySafely() this._hideOverlaySafely()
// 자막 전용 보조 모델은 세션이 끝나면 내린다 — 남겨 두면 받아쓰기 모델과 함께 VRAM 을 잡아 // 자막 전용 보조 모델은 세션이 끝나면 내린다 — 남겨 두면 받아쓰기 모델과 함께 VRAM 을 잡아
// Ollama 로드를 느리게 하거나 OOM 을 부른다. 다음 세션 시작 때 다시 올린다. // 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._refineQueue = []
this._sessionLanguage = null this._sessionLanguage = null
this._session = { ...DEFAULT_START_OPTIONS } this._session = { ...DEFAULT_START_OPTIONS }
this._stopPromise = null
this._setState('inactive') this._setState('inactive')
getSoundEffectService().play('recording-stop') getSoundEffectService().play('recording-stop')
@ -369,8 +401,9 @@ class CaptionService extends EventEmitter {
async dispose(): Promise<void> { async dispose(): Promise<void> {
if (this._disposed) return if (this._disposed) return
this._disposed = true 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() 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 { private _enqueueRefine(segment: CaptionSegment): void {
if (this._disposed || this._state !== 'active') return
if (configGet('captionRefineEnabled') === false) return if (configGet('captionRefineEnabled') === false) return
if (!getLocalLLMService().isAvailable()) return if (!getLocalLLMService().isAvailable()) return
this._refineQueue.push(segment) this._refineQueue.push(segment)
@ -532,17 +575,30 @@ class CaptionService extends EventEmitter {
} }
private async _drainRefineQueue(): Promise<void> { private async _drainRefineQueue(): Promise<void> {
const runId = ++this._refineRunId
const epoch = this._refineEpoch
this._refining = true this._refining = true
try { try {
for (let next = this._refineQueue.shift(); next; next = this._refineQueue.shift()) { while (
await this._refine(next) !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 { } finally {
this._refining = false if (this._refineRunId === runId) {
this._refining = false
this._refineController = null
}
} }
} }
private async _refine(segment: CaptionSegment): Promise<void> { private async _refine(segment: CaptionSegment, epoch: number): Promise<void> {
if (this._disposed || this._state !== 'active' || epoch !== this._refineEpoch) return
const index = this._segments.findIndex((item) => item.id === segment.id) const index = this._segments.findIndex((item) => item.id === segment.id)
if (index === -1) return if (index === -1) return
const previous = this._segments.slice(Math.max(0, index - 2), index).map((item) => item.text) const previous = this._segments.slice(Math.max(0, index - 2), index).map((item) => item.text)
@ -552,19 +608,27 @@ class CaptionService extends EventEmitter {
previous, previous,
...(glossary ? { glossary } : {}), ...(glossary ? { glossary } : {}),
}) })
const controller = new AbortController()
this._refineController = controller
try { try {
const result = await getLocalLLMService().generate(text, { const result = await getLocalLLMService().generate(text, {
systemPrompt, systemPrompt,
temperature: 0, temperature: 0,
maxTokens: Math.min(256, segment.text.length * 2 + 32), maxTokens: Math.min(256, segment.text.length * 2 + 32),
timeoutMs: REFINE_TIMEOUT_MS, timeoutMs: REFINE_TIMEOUT_MS,
signal: controller.signal,
}) })
if (this._disposed || this._state !== 'active' || epoch !== this._refineEpoch) return
const refined = acceptCaptionRefinement(segment.text, result.text) const refined = acceptCaptionRefinement(segment.text, result.text)
if (!refined) return if (!refined) return
// 모델이 용어를 다른 표기로 되돌렸을 수 있다 — 정본 표기를 마지막에 다시 강제한다. // 모델이 용어를 다른 표기로 되돌렸을 수 있다 — 정본 표기를 마지막에 다시 강제한다.
this._replaceSegmentText(segment.id, getTranscriptGlossaryService().correct(refined).text) this._replaceSegmentText(segment.id, getTranscriptGlossaryService().correct(refined).text)
} catch (err) { } catch (err) {
logger.debug(`자막 다듬기 건너뜀: ${err instanceof Error ? err.message : String(err)}`) logger.debug(`자막 다듬기 건너뜀: ${err instanceof Error ? err.message : String(err)}`)
} finally {
if (this._refineController === controller) {
this._refineController = null
}
} }
} }

View file

@ -186,6 +186,7 @@ class LocalLLMService extends EventEmitter {
private _activeRequests = new Set<ActiveRequest>() private _activeRequests = new Set<ActiveRequest>()
private _ensureRunningPromise: Promise<'running' | 'starting' | 'not-installed' | 'failed'> | null = null private _ensureRunningPromise: Promise<'running' | 'starting' | 'not-installed' | 'failed'> | null = null
private _startCheckTimers = new Set<ReturnType<typeof setTimeout>>() private _startCheckTimers = new Set<ReturnType<typeof setTimeout>>()
private _pullControllers = new Set<AbortController>()
private _disposed = false private _disposed = false
get state(): LLMState { get state(): LLMState {
@ -414,7 +415,7 @@ class LocalLLMService extends EventEmitter {
* Ollama 가용성 폴링을 시작한다 (5초 간격). * Ollama 가용성 폴링을 시작한다 (5초 간격).
*/ */
startPolling(): void { startPolling(): void {
if (this._pollInterval) return if (this._pollInterval || this._disposed) return
this._checkAvailability() this._checkAvailability()
this._pollInterval = setInterval(() => { this._pollInterval = setInterval(() => {
if (this._state !== LLMState.Generating) { if (this._state !== LLMState.Generating) {
@ -686,8 +687,13 @@ class LocalLLMService extends EventEmitter {
text: string, text: string,
action: LLMAction, action: LLMAction,
targetLanguage?: string, targetLanguage?: string,
customPrompt?: string customPrompt?: string,
options?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<string> { ): Promise<string> {
if (options?.signal?.aborted) {
throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LLM processing was cancelled')
}
// Phase 11: LLM 처리 쿼터 체크 // Phase 11: LLM 처리 쿼터 체크
try { try {
const { getLicenseService } = await import('./LicenseService') const { getLicenseService } = await import('./LicenseService')
@ -708,10 +714,18 @@ class LocalLLMService extends EventEmitter {
// LicenseService 미초기화 시 허용 // LicenseService 미초기화 시 허용
} }
if (options?.signal?.aborted) {
throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LLM processing was cancelled')
}
const basePrompt = resolveSystemPrompt(action, targetLanguage, customPrompt) const basePrompt = resolveSystemPrompt(action, targetLanguage, customPrompt)
const systemPrompt = `${NO_THINK}\n${basePrompt}` 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) const cleaned = stripReasoningBlocks(result.text)
// reasoning 블록 제거 후 빈 응답이면 원본 텍스트 폴백 // reasoning 블록 제거 후 빈 응답이면 원본 텍스트 폴백
// (모델이 thinking만 하고 출력은 안 한 경우 / 응답 파싱 실패 케이스) // (모델이 thinking만 하고 출력은 안 한 경우 / 응답 파싱 실패 케이스)
@ -784,67 +798,97 @@ class LocalLLMService extends EventEmitter {
} }
private async _doPullModel(modelId: string): Promise<void> { private async _doPullModel(modelId: string): Promise<void> {
if (this._disposed) {
throw new D3ROError(ErrorCode.LLMProcessingCancelled, 'LocalLLMService disposed')
}
const serverUrl = getOllamaServerUrl() const serverUrl = getOllamaServerUrl()
const controller = new AbortController()
this._pullControllers.add(controller)
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null
const onAbort = (): void => {
void reader?.cancel().catch(() => undefined)
}
controller.signal.addEventListener('abort', onAbort, { once: true })
logger.info(`Pull 시작: ${modelId}`) logger.info(`Pull 시작: ${modelId}`)
const response = await fetch(`${serverUrl}/api/pull`, { try {
method: 'POST', const response = await fetch(`${serverUrl}/api/pull`, {
headers: { 'Content-Type': 'application/json' }, method: 'POST',
body: JSON.stringify({ model: modelId, stream: true }) headers: { 'Content-Type': 'application/json' },
}) body: JSON.stringify({ model: modelId, stream: true }),
signal: controller.signal
})
if (!response.ok || !response.body) { if (!response.ok || !response.body) {
throw new D3ROError( throw new D3ROError(
ErrorCode.LLMServerUnreachable, ErrorCode.LLMServerUnreachable,
`Pull 실패: HTTP ${response.status}` `Pull 실패: HTTP ${response.status}`
) )
} }
const reader = response.body.getReader() reader = response.body.getReader()
const decoder = new TextDecoder() if (this._disposed || controller.signal.aborted) {
let buffer = '' throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`)
}
const decoder = new TextDecoder()
let buffer = ''
for (;;) { for (;;) {
const { done, value } = await reader.read() if (this._disposed || controller.signal.aborted) {
if (done) break throw new D3ROError(ErrorCode.LLMProcessingCancelled, `Pull cancelled: ${modelId}`)
}
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true }) buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n') const lines = buffer.split('\n')
buffer = lines.pop() ?? '' buffer = lines.pop() ?? ''
for (const line of lines) { for (const line of lines) {
const trimmed = line.trim() const trimmed = line.trim()
if (trimmed.length === 0) continue if (trimmed.length === 0) continue
try { try {
const chunk = JSON.parse(trimmed) as { const chunk = JSON.parse(trimmed) as {
status: string status: string
digest?: string digest?: string
total?: number total?: number
completed?: number completed?: number
error?: string 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 { getStatus(): LLMStatus {
@ -989,8 +1033,14 @@ class LocalLLMService extends EventEmitter {
clearTimeout(timer) clearTimeout(timer)
} }
this._startCheckTimers.clear() this._startCheckTimers.clear()
for (const controller of this._pullControllers) {
controller.abort()
}
this._pullControllers.clear()
this.stopPolling() this.stopPolling()
this.cancelGeneration() this.cancelGeneration()
this._available = false
this._serverVersion = null
this._refreshState() this._refreshState()
this.removeAllListeners() this.removeAllListeners()
logger.info('LocalLLMService disposed') logger.info('LocalLLMService disposed')
@ -1022,6 +1072,8 @@ class LocalLLMService extends EventEmitter {
// ignore // ignore
} }
if (this._disposed) return
if (!isOk) { if (!isOk) {
try { try {
const tagsRes = await fetch(`${serverUrl}/api/tags`, { const tagsRes = await fetch(`${serverUrl}/api/tags`, {
@ -1035,6 +1087,8 @@ class LocalLLMService extends EventEmitter {
} }
} }
if (this._disposed) return
const wasAvailable = this._available const wasAvailable = this._available
this._available = isOk this._available = isOk
if (detectedVersion) this._serverVersion = detectedVersion if (detectedVersion) this._serverVersion = detectedVersion

View file

@ -243,6 +243,8 @@ class LocalSTTService extends EventEmitter {
// errorEmitted 플래그로 이벤트 중복 방지 // errorEmitted 플래그로 이벤트 중복 방지
private _errorEmitted: boolean = false private _errorEmitted: boolean = false
private _idleUnloadTimer: NodeJS.Timeout | null = null private _idleUnloadTimer: NodeJS.Timeout | null = null
private _downloadControllers = new Set<AbortController>()
private _downloadSleepCancelers = new Set<() => void>()
// ── 상태 접근자 ── // ── 상태 접근자 ──
@ -521,13 +523,22 @@ class LocalSTTService extends EventEmitter {
} }
await this._supervisor.ready() await this._supervisor.ready()
if (this._disposed) {
throw new D3ROError(
ErrorCode.STTModelDownloadCancelled,
'모델 다운로드 중 서비스가 종료되었습니다',
)
}
const startRes = await fetch(`${this._baseUrl}/download`, { const startRes = await this._fetchDownload(
method: 'POST', `${this._baseUrl}/download`,
headers: { 'Content-Type': 'application/json' }, {
body: JSON.stringify({ model_id: modelId }), method: 'POST',
signal: AbortSignal.timeout(10000), headers: { 'Content-Type': 'application/json' },
}) body: JSON.stringify({ model_id: modelId }),
},
10000,
)
// 409 = 이미 다운로드 진행 중 — 실패가 아니라 기존 진행에 합류해 폴링 // 409 = 이미 다운로드 진행 중 — 실패가 아니라 기존 진행에 합류해 폴링
if (!startRes.ok && startRes.status !== 409) { if (!startRes.ok && startRes.status !== 409) {
@ -551,17 +562,33 @@ class LocalSTTService extends EventEmitter {
// 진행률 폴링 — sidecar 통신 실패가 연속되면 중단 // 진행률 폴링 — sidecar 통신 실패가 연속되면 중단
let consecutiveFailures = 0 let consecutiveFailures = 0
for (;;) { for (;;) {
if (this._disposed) {
throw new D3ROError(
ErrorCode.STTModelDownloadCancelled,
`모델 다운로드 취소: ${modelId}`,
)
}
await this._sleep(500) await this._sleep(500)
if (this._disposed) {
throw new D3ROError(
ErrorCode.STTModelDownloadCancelled,
`모델 다운로드 취소: ${modelId}`,
)
}
let status: DownloadStatusResponse let status: DownloadStatusResponse
try { try {
const res = await fetch(`${this._baseUrl}/download/status`, { const res = await this._fetchDownload(`${this._baseUrl}/download/status`, {}, 3000)
signal: AbortSignal.timeout(3000),
})
if (!res.ok) throw new Error(`HTTP ${res.status}`) if (!res.ok) throw new Error(`HTTP ${res.status}`)
status = (await res.json()) as DownloadStatusResponse status = (await res.json()) as DownloadStatusResponse
consecutiveFailures = 0 consecutiveFailures = 0
} catch (err) { } catch (err) {
if (this._disposed) {
throw new D3ROError(
ErrorCode.STTModelDownloadCancelled,
`모델 다운로드 취소: ${modelId}`,
)
}
consecutiveFailures++ consecutiveFailures++
if (consecutiveFailures >= 5) { if (consecutiveFailures >= 5) {
throw new D3ROError( throw new D3ROError(
@ -572,6 +599,13 @@ class LocalSTTService extends EventEmitter {
continue continue
} }
if (this._disposed) {
throw new D3ROError(
ErrorCode.STTModelDownloadCancelled,
`모델 다운로드 취소: ${modelId}`,
)
}
this._emitDownloadProgress( this._emitDownloadProgress(
modelId, modelId,
status.percent, status.percent,
@ -637,6 +671,14 @@ class LocalSTTService extends EventEmitter {
logger.info('LocalSTTService dispose 시작') logger.info('LocalSTTService dispose 시작')
this._clearIdleUnload() 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._initializing = null
this._modelReady = false this._modelReady = false
this._auxModelId = null this._auxModelId = null
@ -995,8 +1037,38 @@ class LocalSTTService extends EventEmitter {
logger.error(`STT 에러: [${error.code}] ${error.message}`) logger.error(`STT 에러: [${error.code}] ${error.message}`)
} }
private async _fetchDownload(
url: string,
init: RequestInit,
timeoutMs: number,
): Promise<Response> {
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<void> { private _sleep(ms: number): Promise<void> {
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)
})
} }
// ── 타입 안전한 이벤트 메서드 오버라이드 ── // ── 타입 안전한 이벤트 메서드 오버라이드 ──

View file

@ -49,6 +49,7 @@ class VoiceConversationService extends EventEmitter {
* 카운트가 쌓였고, 세션을 끝내도 마이크(SoX)가 계속 열려 있었다. * 카운트가 쌓였고, 세션을 끝내도 마이크(SoX)가 계속 열려 있었다.
*/ */
private _micHeld = false private _micHeld = false
private _listenEpoch = 0
private _responseAbortController: AbortController | null = null private _responseAbortController: AbortController | null = null
/** 취소·세션 종료마다 올린다 — 전사가 끝났을 때 그 사이 취소됐는지 가린다(전사 중엔 abort 할 응답이 없다). */ /** 취소·세션 종료마다 올린다 — 전사가 끝났을 때 그 사이 취소됐는지 가린다(전사 중엔 abort 할 응답이 없다). */
private _turn = 0 private _turn = 0
@ -122,8 +123,10 @@ class VoiceConversationService extends EventEmitter {
this._turn += 1 this._turn += 1
this._stopListening() this._stopListening()
// 이 대화의 응답만 멈춘다 — signal 이 LLM 호출까지 전달된다(전역 cancelGeneration 은 다른 기능의 // 이 대화의 응답만 멈춘다 — signal 이 LLM 호출까지 전달된다(전역 cancelGeneration 은 다른 기능의
// 진행 중 호출까지 끊었다). // 진행 중 호출까지 끊었다). 포인터를 먼저 비워 취소 직후 새 턴이 막히지 않게 한다.
this._responseAbortController?.abort() const responseController = this._responseAbortController
this._responseAbortController = null
responseController?.abort()
getTTSPlaybackService().stop() getTTSPlaybackService().stop()
this._isActive = false this._isActive = false
@ -147,7 +150,9 @@ class VoiceConversationService extends EventEmitter {
*/ */
cancelResponse(): void { cancelResponse(): void {
this._turn += 1 this._turn += 1
this._responseAbortController?.abort() const responseController = this._responseAbortController
this._responseAbortController = null
responseController?.abort()
getTTSPlaybackService().stop() getTTSPlaybackService().stop()
if (this._isActive && this._state !== 'listening') { if (this._isActive && this._state !== 'listening') {
this._resumeListening() this._resumeListening()
@ -266,7 +271,9 @@ class VoiceConversationService extends EventEmitter {
if (!this._micHeld) { if (!this._micHeld) {
this._micHeld = true this._micHeld = true
const listenEpoch = ++this._listenEpoch
audioService.start().catch((err) => { audioService.start().catch((err) => {
if (!this._isActive || this._listenEpoch !== listenEpoch) return
this._micHeld = false this._micHeld = false
logger.error('Failed to start audio capture for conversation:', err) logger.error('Failed to start audio capture for conversation:', err)
this._emitError('stt', 'Failed to start microphone') this._emitError('stt', 'Failed to start microphone')
@ -278,6 +285,7 @@ class VoiceConversationService extends EventEmitter {
} }
private _stopListening(): void { private _stopListening(): void {
this._listenEpoch += 1
const audioService = getAudioCaptureService() const audioService = getAudioCaptureService()
if (this._audioListenerBound) { if (this._audioListenerBound) {
audioService.off('audio-data', this._onAudioData) audioService.off('audio-data', this._onAudioData)

View file

@ -1023,8 +1023,10 @@ class VoiceModeService extends EventEmitter {
action: LLMAction, action: LLMAction,
targetLanguage?: string, targetLanguage?: string,
customPrompt?: string, customPrompt?: string,
signal?: AbortSignal,
): Promise<string> { ): Promise<string> {
return getLlmGateway().processText(text, action, targetLanguage, customPrompt, { return getLlmGateway().processText(text, action, targetLanguage, customPrompt, {
...(signal ? { signal } : {}),
onFallback: (reason) => this._emitPremiumFallback(reason), onFallback: (reason) => this._emitPremiumFallback(reason),
}) })
} }
@ -1087,8 +1089,8 @@ class VoiceModeService extends EventEmitter {
const processedText = const processedText =
route.kind === 'instruction' route.kind === 'instruction'
? await this._processWithInstruction(userText, route.instructionId) ? await this._processWithInstruction(userText, route.instructionId, run.controller.signal)
: await this._processWithAction(userText, route.action) : await this._processWithAction(userText, route.action, run.controller.signal)
if (!this._isCurrent(run)) return if (!this._isCurrent(run)) return
@ -1115,7 +1117,11 @@ class VoiceModeService extends EventEmitter {
} }
/** 지시문(음성 단축키 또는 활성 지시문·파이프라인)으로 처리한다. 지시문이 없으면 지시문 없이 custom 처리 */ /** 지시문(음성 단축키 또는 활성 지시문·파이프라인)으로 처리한다. 지시문이 없으면 지시문 없이 custom 처리 */
private async _processWithInstruction(userText: string, instructionId: string | null): Promise<string> { private async _processWithInstruction(
userText: string,
instructionId: string | null,
signal?: AbortSignal,
): Promise<string> {
let invocationText = userText let invocationText = userText
let instructionPrompt: string | undefined 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/…)으로 처리한다 */ /** 일반 액션(refine/translate/…)으로 처리한다 */
private async _processWithAction( private async _processWithAction(
userText: string, userText: string,
action: Extract<LlmProcessingRoute, { kind: 'action' }>['action'], action: Extract<LlmProcessingRoute, { kind: 'action' }>['action'],
signal?: AbortSignal,
): Promise<string> { ): Promise<string> {
const targetLanguage = resolveTargetLanguage() const targetLanguage = resolveTargetLanguage()
logger.info( logger.info(
@ -1164,6 +1171,8 @@ class VoiceModeService extends EventEmitter {
userText, userText,
action, action,
action === 'translate' ? targetLanguage : undefined, action === 'translate' ? targetLanguage : undefined,
undefined,
signal,
) )
} }

View file

@ -114,7 +114,13 @@ export interface PremiumLlmAdapter {
export interface LocalLlmAdapter { export interface LocalLlmAdapter {
isAvailable(): boolean isAvailable(): boolean
processText(text: string, action: LLMAction, targetLanguage?: string, customPrompt?: string): Promise<string> processText(
text: string,
action: LLMAction,
targetLanguage?: string,
customPrompt?: string,
options?: { signal?: AbortSignal; timeoutMs?: number },
): Promise<string>
generate( generate(
text: string, text: string,
options?: LlmGenerateOptions & { signal?: AbortSignal; timeoutMs?: number }, options?: LlmGenerateOptions & { signal?: AbortSignal; timeoutMs?: number },
@ -214,8 +220,10 @@ class RoutedLlmGateway implements LlmGateway {
} }
} }
throwIfAborted(hooks?.signal) throwIfAborted(hooks?.signal)
// 로컬 processText 는 호출 단위 signal 을 받지 않는다 — 끝난 뒤 취소됐으면 결과를 버린다. const perCall = callOptions(hooks)
const result = await this._deps.getLocal().processText(text, action, targetLanguage, customPrompt) 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) throwIfAborted(hooks?.signal)
return result return result
} }

View file

@ -567,6 +567,8 @@ ipcMain.on(IPC_CHANNELS.POPUP_RESULT.MEASURED, (event: Electron.IpcMainEvent, da
}) })
export function hideResultPopup(): void { export function hideResultPopup(): void {
cancelPendingResultShow()
lastResultCursor = null
if (resultPopupWindow && !resultPopupWindow.isDestroyed()) { if (resultPopupWindow && !resultPopupWindow.isDestroyed()) {
resultPopupWindow.hide() resultPopupWindow.hide()
} }

View file

@ -6,6 +6,7 @@
// - realtime-token 은 주입된 자격 증명 포트로 호출한다 (item 2) // - realtime-token 은 주입된 자격 증명 포트로 호출한다 (item 2)
import { beforeEach, describe, expect, it, vi } from 'vitest' import { beforeEach, describe, expect, it, vi } from 'vitest'
import { desktopCapturer, session } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
const handlers = vi.hoisted(() => new Map<string, (event: unknown, params?: unknown) => Promise<unknown>>()) const handlers = vi.hoisted(() => new Map<string, (event: unknown, params?: unknown) => Promise<unknown>>())
@ -136,3 +137,25 @@ describe('realtime-token 은 자격 증명 포트를 쓴다', () => {
expect(invokeFn).toHaveBeenCalledWith('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<string, unknown>) => void) => Promise<void>)
| 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({})
})
})

View file

@ -190,4 +190,64 @@ describe('VoiceConversationService local response ownership', () => {
expect(mocks.speakSentences).not.toHaveBeenCalled() expect(mocks.speakSentences).not.toHaveBeenCalled()
expect(service.getHistory().map((message) => message.content)).toEqual(['cancel before response']) 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<string, string> {
await new Promise<void>((resolve) => {
releaseFirstStream = resolve
})
return ''
})())
.mockImplementationOnce(() => (async function* (): AsyncGenerator<string, string> {
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<void>((_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)
})
}) })

View file

@ -261,6 +261,36 @@ describe('VoiceModeService', () => {
expect(cancelReason).toBe('user') 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<string>((_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 후처리 인자 전달', () => { describe('LLM 후처리 인자 전달', () => {

View file

@ -7,6 +7,7 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { spawn } from 'child_process'
import { ipcMain } from 'electron' import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels' import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
@ -228,3 +229,37 @@ describe('마이크 테스트 IPC', () => {
expect(proc(0).kill).toHaveBeenCalledTimes(1) 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)
})
})

View file

@ -16,11 +16,12 @@ vi.mock('../../../src/main/services/ConfigService', () => ({
const stt = vi.hoisted(() => ({ const stt = vi.hoisted(() => ({
gate: null as Promise<void> | null, gate: null as Promise<void> | null,
initialize: vi.fn(async () => undefined),
releaseAuxModel: vi.fn(async () => undefined), releaseAuxModel: vi.fn(async () => undefined),
})) }))
vi.mock('../../../src/main/services/LocalSTTService', () => ({ vi.mock('../../../src/main/services/LocalSTTService', () => ({
getLocalSTTService: () => ({ getLocalSTTService: () => ({
initialize: vi.fn(async () => undefined), initialize: stt.initialize,
ensureAuxModel: vi.fn(async () => { ensureAuxModel: vi.fn(async () => {
if (stt.gate) await stt.gate 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', () => ({ vi.mock('../../../src/main/services/AudioCaptureService', () => ({
getAudioCaptureService: () => ({ ...audio, on: vi.fn(), off: vi.fn() }), getAudioCaptureService: () => ({ ...audio, on: vi.fn(), off: vi.fn() }),
calculateRMS: () => 0.1, calculateRMS: () => 0.1,
@ -63,7 +70,12 @@ beforeEach(async () => {
vi.clearAllMocks() vi.clearAllMocks()
config.values = { captionSttModelId: 'caption-small' } config.values = { captionSttModelId: 'caption-small' }
stt.gate = null stt.gate = null
stt.initialize.mockImplementation(async () => undefined)
audio.stopGate = null
audio.start.mockImplementation(async () => undefined) audio.start.mockImplementation(async () => undefined)
audio.stop.mockImplementation(async () => {
if (audio.stopGate) await audio.stopGate
})
mod = await import('../../../src/main/services/CaptionService') mod = await import('../../../src/main/services/CaptionService')
mod.resetCaptionServiceForTests() mod.resetCaptionServiceForTests()
}) })
@ -98,4 +110,42 @@ describe('CaptionService — 자막 전용 보조 모델 해제', () => {
expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1) expect(stt.releaseAuxModel).toHaveBeenCalledTimes(1)
expect(caption.getState()).toBe('inactive') 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()
})
}) })

View file

@ -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<string, unknown> }))
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<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,
}))
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<void> | 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<void> {}
async flush(): Promise<void> {
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<void>((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<AbortSignal | undefined> = []
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()
})
})

View file

@ -151,4 +151,17 @@ describe('LlmGateway 호출 단위 취소', () => {
}) })
expect(l.processText).not.toHaveBeenCalled() 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 })
})
}) })

View file

@ -256,3 +256,121 @@ describe('LocalLLMService model residency (2026-10-11 memory policy)', () => {
expect(unloads).toEqual([{ model: 'gemma4:e4b', keep_alive: 0 }]) 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)
})
})

View file

@ -374,4 +374,54 @@ describe('LocalSTTService — 사이드카 크래시 재시작', () => {
expect(local.currentModelId).toBe('tiny') expect(local.currentModelId).toBe('tiny')
await local.dispose() 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)
})
}) })

View file

@ -139,4 +139,23 @@ describe('WindowManager popup hide/show timer race regressions', () => {
await vi.advanceTimersByTimeAsync(160) await vi.advanceTimersByTimeAsync(160)
expect(wm.isCommandPopupVisible()).toBe(false) 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)
})
}) })