mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 11:24:37 +00:00
fix(speech): restart the speech process when a canceled decode outlasts a short grace
A cancel now answers at once, so a decode already running for the canceled stream (hung in native code, or just long) could hold the queue for the next dictation. The host waits up to 2 s for the speech process to reach the discard; past that, with no other dictation on it, the process restarts, and a dictation starting meanwhile waits for that instead of reusing it.
This commit is contained in:
@@ -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<number> | 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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<void> | null
|
||||
}
|
||||
|
||||
export type SttSession = {
|
||||
|
||||
@@ -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<boolean> {
|
||||
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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user