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

@ -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<typeof setInterval> | 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()