diff --git a/src/main/speech/stt-service.test.ts b/src/main/speech/stt-service.test.ts index 62db37c11ba..e3864b1e8a0 100644 --- a/src/main/speech/stt-service.test.ts +++ b/src/main/speech/stt-service.test.ts @@ -13,6 +13,8 @@ const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.ho terminated = false emitStoppedOnStop = true answerDiscard = true + /** False: a decode for the canceled stream never ends, so the queue never reaches its discard. */ + reachDiscard = true emitReadyOnInit = HoistedMockWorker.emitReadyOnInit emitExitOnTerminate = true terminatePromise: Promise | null = null @@ -64,6 +66,11 @@ const { MockWorker, getCreatedWorkerCount, getLastWorker, resetWorkers } = vi.ho ) { queueMicrotask(() => this.emit('message', { type: 'stopped', streamId: message.streamId })) } + if (message.type === 'discard' && this.answerDiscard && this.reachDiscard) { + queueMicrotask(() => + this.emit('message', { type: 'discarded', streamId: message.streamId }) + ) + } } attachAudioPort(streamId: string, port: unknown): boolean { @@ -479,6 +486,26 @@ describe('SttService', () => { expect(sink).not.toHaveBeenCalledWith(expect.objectContaining({ type: 'error' })) }) + it('restarts the speech process when a canceled decode is still running as the next dictation starts', async () => { + vi.useFakeTimers() + try { + const service = new SttService(readyModels(), spawnMockWorker) + await service.startDictation('model-a', vi.fn(), undefined, 'desktop') + const hung = getLastWorker()! + hung.reachDiscard = false + await service.stopDictation('desktop', { discard: true }) + + const next = service.startDictation('model-a', vi.fn(), undefined, 'desktop') + await vi.advanceTimersByTimeAsync(2_000) + await next + + expect(hung.terminated).toBe(true) + expect(getLastWorker()).not.toBe(hung) + } finally { + vi.useRealTimers() + } + }) + it('drops in-flight audio while stop is waiting for the worker', async () => { const service = new SttService(readyModels(), spawnMockWorker) diff --git a/src/main/speech/stt-session-start.ts b/src/main/speech/stt-session-start.ts index ee7bd5b1833..fad17af1f9d 100644 --- a/src/main/speech/stt-session-start.ts +++ b/src/main/speech/stt-session-start.ts @@ -154,6 +154,10 @@ async function acquireSttEngine( const current = state.engine // Bound in the same step it is chosen, so an idle teardown never sees it unused. if (current && sameSttRecognizerConfig(current, config)) { + if (current.draining) { + await current.draining + continue + } clearSttIdleTeardownTimer(state) return bindSttEngine(session, current) } @@ -212,6 +216,7 @@ export function spawnSttEngine( retired: false, vadModelPath, onUnloaded: [], + draining: null, ready: readyPromise.catch((error: unknown) => { failSttEngine(state, engine, STT_WORKER_EXITED_ERROR) throw error diff --git a/src/main/speech/stt-session-state.ts b/src/main/speech/stt-session-state.ts index c54d4dd5eed..2bdd8a022f9 100644 --- a/src/main/speech/stt-session-state.ts +++ b/src/main/speech/stt-session-state.ts @@ -23,6 +23,8 @@ export type SttEngine = SttRecognizerConfig & { vadModelPath: string | undefined /** Called once this process is gone; set when it must not outlive its last dictation. */ onUnloaded: (() => void)[] + /** Set while a canceled stream's last decode finishes; a new dictation waits for it before reusing this process. */ + draining: Promise | null } export type SttSession = { diff --git a/src/main/speech/stt-session-stop.ts b/src/main/speech/stt-session-stop.ts index 80efc46ed66..1f0310b2984 100644 --- a/src/main/speech/stt-session-stop.ts +++ b/src/main/speech/stt-session-stop.ts @@ -30,7 +30,9 @@ export function stopSttDictation( // A cancel after Finish still drops the work whose text it would throw away. if (options.discard) { session.cloud?.cancel() - session.engine?.worker.postMessage({ type: 'discard', streamId: session.streamId }) + if (session.engine) { + discardLocalStream(state, session.engine, session.streamId) + } } return session.stopPromise ?? Promise.resolve() } @@ -79,10 +81,66 @@ function stopLocalSession( discard, finish: (outcome) => finishLocalStop(state, session, engine, outcome, discard) }) - engine.worker.postMessage({ type: discard ? 'discard' : 'stop', streamId: session.streamId }) + if (discard) { + discardLocalStream(state, engine, session.streamId) + } else { + engine.worker.postMessage({ type: 'stop', streamId: session.streamId }) + } return stopped } +/** + * The speech process answers a cancel at once and skips the stream's queued work, but a decode already running + * finishes first. One that outlasts the grace (hung in native code, or just long) restarts the process, as a + * cancel always did, so the next dictation isn't stuck behind it. + */ +function discardLocalStream(state: SttSessionState, engine: SttEngine, streamId: string): void { + const reached = waitForSttWorkerMessage( + engine.worker, + (message) => message.type === 'discarded' && message.streamId === streamId, + DISCARD_DICTATION_TIMEOUT_MS + ) + engine.worker.postMessage({ type: 'discard', streamId }) + const draining = reached.then(async (inTime) => { + // Another device's decode may hold the queue; never kill its dictation. + if (!inTime && state.engine === engine && sttEngineUsers(state, engine).length === 0) { + await teardownSttEngine(state, engine) + } + }) + engine.draining = draining + void draining.finally(() => { + if (engine.draining === draining) { + engine.draining = null + } + }) +} + +/** Resolves true when the worker sends a matching message (or exits), false after `timeoutMs`. */ +function waitForSttWorkerMessage( + worker: SttEngine['worker'], + matches: (message: { type: string; streamId?: string }) => boolean, + timeoutMs: number +): Promise { + return new Promise((resolve) => { + const settle = (inTime: boolean): void => { + clearTimeout(timer) + worker.off('message', onMessage) + worker.off('exit', onExit) + resolve(inTime) + } + const onMessage = (message: { type: string; streamId?: string }): void => { + if (matches(message)) { + settle(true) + } + } + const onExit = (): void => settle(true) + const timer = setTimeout(() => settle(false), timeoutMs) + timer.unref?.() + worker.on('message', onMessage) + worker.on('exit', onExit) + }) +} + function finishLocalStop( state: SttSessionState, session: SttSession, diff --git a/src/main/speech/stt-worker-cpu-fallback.ts b/src/main/speech/stt-worker-cpu-fallback.ts index d86c3d1b15b..ede1d976006 100644 --- a/src/main/speech/stt-worker-cpu-fallback.ts +++ b/src/main/speech/stt-worker-cpu-fallback.ts @@ -17,7 +17,7 @@ type ChildMessage = | { type: 'chunk'; streamId: string; text: string; samples: number } | { type: 'speech-pending'; streamId: string; pending: boolean } | { - type: 'ready' | 'loading' | 'stopped' | 'error' | 'partial' | 'final' + type: 'ready' | 'loading' | 'stopped' | 'discarded' | 'error' | 'partial' | 'final' streamId?: string text?: string error?: string diff --git a/src/main/speech/stt-worker.ts b/src/main/speech/stt-worker.ts index e1107d04329..c85146e3bd2 100644 --- a/src/main/speech/stt-worker.ts +++ b/src/main/speech/stt-worker.ts @@ -120,10 +120,11 @@ function cancelStream(streamId: string): void { parentPort.post({ type: 'stopped', streamId }) } -// Runs in queue order, after everything queued for the stream before the cancel. +// Runs in queue order, after everything queued for the stream before the cancel; the host waits for it. function handleDiscard(streamId: string): void { discarded.delete(streamId) recognizer?.discard(streamId) + parentPort.post({ type: 'discarded', streamId }) } function handleMessage(