Skip to content

Commit 700ec70

Browse files
committed
ensure resampler cleanup happens on errors
1 parent ce3909f commit 700ec70

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

agents/src/tts/fallback_adapter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ class FallbackChunkedStream extends ChunkedStream {
324324
this.adapter.markUnAvailable(i);
325325
continue;
326326
}
327+
const resampler = this.adapter.createResamplerForTTS(i);
328+
327329
try {
328330
this._logger.debug({ tts: tts.label }, 'attempting TTS synthesis');
329331
const connOptions: APIConnectOptions = {
@@ -332,7 +334,6 @@ class FallbackChunkedStream extends ChunkedStream {
332334
};
333335
const stream = tts.synthesize(this.inputText, connOptions, this.abortSignal);
334336
let audioReceived = false;
335-
const resampler = this.adapter.createResamplerForTTS(i);
336337
for await (const audio of stream) {
337338
if (this.abortController.signal.aborted) {
338339
stream.close();
@@ -366,7 +367,6 @@ class FallbackChunkedStream extends ChunkedStream {
366367
});
367368
audioReceived = true;
368369
}
369-
resampler.close();
370370
}
371371

372372
// Verify audio was actually received - silent failures should trigger fallback
@@ -385,6 +385,8 @@ class FallbackChunkedStream extends ChunkedStream {
385385
} else {
386386
throw error;
387387
}
388+
} finally {
389+
resampler?.close();
388390
}
389391
}
390392
const labels = this.adapter.ttsInstances.map((t) => t.label).join(', ');
@@ -444,6 +446,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
444446
this.adapter.markUnAvailable(i);
445447
continue;
446448
}
449+
const resampler = this.adapter.createResamplerForTTS(i);
447450

448451
try {
449452
this._logger.debug({ tts: originalTts.label }, 'attempting TTS stream');
@@ -454,7 +457,6 @@ class FallbackSynthesizeStream extends SynthesizeStream {
454457
};
455458

456459
const stream = tts.stream({ connOptions });
457-
const resampler = this.adapter.createResamplerForTTS(i);
458460
let bufferIndex = 0;
459461
let streamOutputCompleted = false;
460462
const forwardBufferToTTS = async () => {
@@ -520,7 +522,6 @@ class FallbackSynthesizeStream extends SynthesizeStream {
520522
});
521523
this.audioPushed = true;
522524
}
523-
resampler.close();
524525
}
525526
} finally {
526527
// processOutput and forwardBufferToTTS run in parallel.
@@ -530,6 +531,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
530531
// keep polling indefinitely, blocking fallback to the next TTS.
531532
// This flag tells it to exit early.
532533
streamOutputCompleted = true;
534+
resampler?.close();
533535
}
534536
};
535537
const [outputResult, forwardBufferResult] = await Promise.allSettled([

agents/src/voice/generation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,6 @@ async function forwardAudio(
839839
for (const f of resampler.flush()) {
840840
await audioOutput.captureFrame(f);
841841
}
842-
resampler.close();
843842
}
844843
} catch (e) {
845844
if (e instanceof IdleTimeoutError) {
@@ -856,6 +855,7 @@ async function forwardAudio(
856855

857856
reader?.releaseLock();
858857
audioOutput.flush();
858+
resampler?.close();
859859
}
860860
}
861861

0 commit comments

Comments
 (0)