Skip to content

Commit 93686d6

Browse files
fix(inference): treat a dropped TTS gateway session as a failed attempt
`session.closed` part-way through a reply was treated as successful completion. The rest of the reply's text was then discarded with no error and no retry, and the gateway websocket went back into the ConnectionPool mid-synthesis, so the next reply read the previous reply's outstanding audio as its own. Flush the audio the dropped session produced, mark that segment's last frame final, and reject with a retryable APIStatusError so the socket is evicted and the retry finishes the reply. Gate socket reuse on having observed `done`. The cancelled attempt's input task reads with its abort signal so it stops waiting instead of consuming the text the retry needs. Python has no session.closed branch: the drop surfaces as a timeout or closed socket, the exception leaves _run, and the pool evicts the connection. Resolving it was the divergence. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent d4edb58 commit 93686d6

4 files changed

Lines changed: 430 additions & 5 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@livekit/agents': patch
3+
---
4+
5+
fix(inference): treat a dropped TTS gateway session as a failed attempt
6+
7+
When the inference gateway ends a TTS session with `session.closed` part-way through a
8+
reply, the JS client treated it as a successful completion. That single mistake had two
9+
consequences. The rest of the reply's text was discarded with no error, no warning and no
10+
retry — in the trace this came from, a ~9000-character reply had only 2017 characters
11+
submitted before the drop. And the gateway websocket went back into the `ConnectionPool`
12+
while the gateway was still mid-synthesis, so the next reply picked it up and read the
13+
previous reply's outstanding audio as its own: after one barge-in the following reply spoke
14+
53.8s of the previous answer while the transcript showed the new one.
15+
16+
The dropped session now flushes the audio it did produce, marks that segment's last frame
17+
final, and rejects with a retryable `APIStatusError`, so the socket is evicted and the retry
18+
finishes the reply. Socket reuse is additionally gated on having observed the gateway's
19+
`done`.

agents/src/inference/tts.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ export class SynthesizeStream<TModel extends TTSModels> extends BaseSynthesizeSt
581581
protected async run(): Promise<void> {
582582
let closing = false;
583583
let lastFrame: AudioFrame | undefined;
584+
// Only a `done` from the gateway proves the session owes us no more audio, and a socket
585+
// recycled before that hands the leftover audio to whichever SynthesizeStream picks it
586+
// up next. `session.closed` is the exit that reaches the pool: it returns from this run
587+
// normally, so nothing else evicts the socket. The remaining non-`done` exits — a closed
588+
// event channel, a swallowed abort — are only ever reached after `onClose` / `onAbort`
589+
// has already removed the socket, so gating reuse on `done` is what keeps reuse tied to
590+
// the one event that proves the session is drained rather than to each exit remembering.
591+
let sessionDrained = false;
584592
// Timestamps are delivered in their own WS message; buffer them and attach
585593
// to the next audio frame that we forward to the output emitter. This
586594
// mirrors the semantics of `output_emitter.push_timed_transcript` on the
@@ -631,13 +639,16 @@ export class SynthesizeStream<TModel extends TTSModels> extends BaseSynthesizeSt
631639
};
632640

633641
const createInputTask = async (signal: AbortSignal) => {
634-
for await (const data of this.input) {
635-
if (signal.aborted || closing) break;
636-
if (data === SynthesizeStream.FLUSH_SENTINEL) {
642+
while (!signal.aborted && !closing) {
643+
// Read with the signal so a cancelled attempt stops waiting instead of taking —
644+
// and dropping — the next chunk of text, which belongs to the retry.
645+
const { done, value } = await this.input.next({ signal });
646+
if (done) break;
647+
if (value === SynthesizeStream.FLUSH_SENTINEL) {
637648
sendTokenizerStream.flush();
638649
continue;
639650
}
640-
sendTokenizerStream.pushText(data);
651+
sendTokenizerStream.pushText(value);
641652
}
642653
// Only call endInput if the stream hasn't been closed by cleanup
643654
if (!closing) {
@@ -830,12 +841,29 @@ export class SynthesizeStream<TModel extends TTSModels> extends BaseSynthesizeSt
830841
}
831842
sendLastFrame(currentSessionId!, true);
832843
this.queue.put(SynthesizeStream.END_OF_STREAM);
844+
sessionDrained = true;
833845
await resourceCleanup();
834846
completionFuture.resolve();
835847
return;
836848
case 'session.closed':
849+
// The gateway dropped the session before it finished the reply. Hand over
850+
// the audio it did produce, then fail the attempt: Python has no
851+
// `session.closed` branch at all, so the dropped session surfaces there as
852+
// a read timeout or a closed socket, i.e. an error that evicts the socket
853+
// and lets the retry machinery resynthesize what is left. Resolving here
854+
// instead reports a truncated reply as a completed one.
855+
for (const frame of bstream.flush()) {
856+
sendLastFrame(currentSessionId!, false);
857+
lastFrame = frame;
858+
}
859+
sendLastFrame(currentSessionId!, true);
837860
await resourceCleanup();
838-
completionFuture.resolve();
861+
completionFuture.reject(
862+
new APIStatusError({
863+
message: 'Gateway closed the TTS session before synthesis completed',
864+
options: { requestId },
865+
}),
866+
);
839867
return;
840868
case 'error':
841869
this.#logger.error(
@@ -920,6 +948,9 @@ export class SynthesizeStream<TModel extends TTSModels> extends BaseSynthesizeSt
920948
await resourceCleanup();
921949
await cancelAndWait(tasks, 5000);
922950
this.abortController.signal.removeEventListener('abort', onStreamAbort);
951+
if (!sessionDrained) {
952+
this.tts.pool.remove(ws);
953+
}
923954
}
924955
} catch (e) {
925956
// If aborted, don't throw - let cleanup handle it
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
import type { AddressInfo } from 'node:net';
5+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
6+
import { WebSocketServer } from 'ws';
7+
import type { WebSocket as WsSocket } from 'ws';
8+
import { initializeLogger } from '../log.js';
9+
import { TTS } from './tts.js';
10+
11+
/**
12+
* Pins the user-visible invariant: a reply must never inherit the audio a previous, dropped
13+
* session never finished delivering.
14+
*
15+
* `session.closed` is the only route that reaches that leak. Once the `session.closed`
16+
* handler rejects the attempt instead of resolving it, the exception path in
17+
* `ConnectionPool.withConnection` evicts the socket by itself and this test passes with or
18+
* without the `sessionDrained` eviction in `SynthesizeStream.run`. Read it as coverage of
19+
* the behaviour, not of that eviction.
20+
*/
21+
22+
initializeLogger({ pretty: false });
23+
24+
const SAMPLE_RATE = 16000;
25+
const FRAME_MS = 20;
26+
const SAMPLES_PER_FRAME = (SAMPLE_RATE * FRAME_MS) / 1000;
27+
28+
/** Audio from the session that gets dropped and audio from a healthy session carry
29+
* distinct constant samples so the test can tell, per frame, which session's synthesis a
30+
* frame actually came from. */
31+
const DROPPED_SESSION_SAMPLE = 1000;
32+
const HEALTHY_SESSION_SAMPLE = 2000;
33+
34+
/** ~6s of already-synthesized audio the gateway still owes after it drops the session. */
35+
const BACKLOG_FRAMES = 300;
36+
const OWN_FRAMES = 25;
37+
38+
function audioEvent(sessionId: string, sample: number): string {
39+
const pcm = Buffer.alloc(SAMPLES_PER_FRAME * 2);
40+
for (let i = 0; i < SAMPLES_PER_FRAME; i++) {
41+
pcm.writeInt16LE(sample, i * 2);
42+
}
43+
return JSON.stringify({
44+
type: 'output_audio',
45+
session_id: sessionId,
46+
audio: pcm.toString('base64'),
47+
});
48+
}
49+
50+
/**
51+
* Gateway stand-in for the production trace in which the first reply's session was dropped
52+
* with `session.closed` while ~90s of synthesis was still outstanding. On the first
53+
* connection it streams a short prefix, drops the session without a `done`, then keeps
54+
* flushing the rest of the backlog onto the same socket. Any later connection behaves
55+
* normally.
56+
*/
57+
async function startFakeGateway() {
58+
const wss = new WebSocketServer({ port: 0, host: '127.0.0.1' });
59+
await new Promise<void>((resolve) => wss.once('listening', () => resolve()));
60+
let connections = 0;
61+
const sockets: WsSocket[] = [];
62+
63+
wss.on('connection', (ws: WsSocket) => {
64+
sockets.push(ws);
65+
const index = ++connections;
66+
const sessionId = `session-${index}`;
67+
const sample = index === 1 ? DROPPED_SESSION_SAMPLE : HEALTHY_SESSION_SAMPLE;
68+
let dropped = false;
69+
70+
const send = (payload: string) => {
71+
if (ws.readyState === ws.OPEN) ws.send(payload);
72+
};
73+
74+
ws.on('message', async (raw: Buffer) => {
75+
const event = JSON.parse(raw.toString()) as { type: string };
76+
if (event.type === 'session.create') {
77+
send(JSON.stringify({ type: 'session.created', session_id: sessionId }));
78+
return;
79+
}
80+
if (event.type !== 'session.flush') return;
81+
82+
// A healthy session serves every reply it is asked for, so a pooled socket can be
83+
// reused across replies.
84+
if (index > 1) {
85+
for (let i = 0; i < OWN_FRAMES; i++) send(audioEvent(sessionId, sample));
86+
send(JSON.stringify({ type: 'done', session_id: sessionId }));
87+
return;
88+
}
89+
90+
if (dropped) return;
91+
dropped = true;
92+
93+
// Reply 1: hand over a short prefix, then drop the session mid-synthesis.
94+
for (let i = 0; i < OWN_FRAMES; i++) send(audioEvent(sessionId, sample));
95+
send(JSON.stringify({ type: 'session.closed', session_id: sessionId }));
96+
// The synthesis that was already in flight keeps arriving on this socket.
97+
await new Promise((resolve) => setTimeout(resolve, 20));
98+
for (let i = 0; i < BACKLOG_FRAMES; i++) send(audioEvent(sessionId, sample));
99+
send(JSON.stringify({ type: 'done', session_id: sessionId }));
100+
});
101+
});
102+
103+
const { port } = wss.address() as AddressInfo;
104+
return {
105+
baseURL: `http://127.0.0.1:${port}/v1`,
106+
get connections() {
107+
return connections;
108+
},
109+
close: () => {
110+
for (const socket of sockets) socket.terminate();
111+
return new Promise<void>((resolve) => wss.close(() => resolve()));
112+
},
113+
};
114+
}
115+
116+
async function synthesize(tts: TTS<string>, text: string) {
117+
const stream = tts.stream();
118+
stream.pushText(text);
119+
stream.endInput();
120+
121+
const samples = new Set<number>();
122+
let frames = 0;
123+
for await (const event of stream) {
124+
if (typeof event === 'symbol' || event.frame.samplesPerChannel === 0) continue;
125+
frames++;
126+
samples.add(event.frame.data[0]!);
127+
}
128+
await stream.close();
129+
return { frames, samples };
130+
}
131+
132+
describe('inference TTS pooled socket reuse', () => {
133+
let gateway: Awaited<ReturnType<typeof startFakeGateway>>;
134+
135+
beforeEach(async () => {
136+
gateway = await startFakeGateway();
137+
});
138+
139+
afterEach(async () => {
140+
await gateway.close();
141+
});
142+
143+
it('does not hand a dropped session\u2019s outstanding audio to the next reply', async () => {
144+
const tts = new TTS({
145+
model: 'inworld/inworld-tts-2',
146+
voice: 'Sarah',
147+
sampleRate: SAMPLE_RATE,
148+
baseURL: gateway.baseURL,
149+
apiKey: 'devkey',
150+
apiSecret: 'secret'.padEnd(32, 'x'),
151+
});
152+
153+
// The dropped session fails the attempt, so the first reply is the prefix it did
154+
// deliver plus the audio from the retry that finishes it on a healthy session.
155+
const first = await synthesize(tts, 'Tell me a long story about the lighthouse.');
156+
expect(first.samples).toEqual(new Set([DROPPED_SESSION_SAMPLE, HEALTHY_SESSION_SAMPLE]));
157+
158+
const second = await synthesize(tts, 'Tell me a long joke about skeletons.');
159+
160+
// The second reply must speak only its own synthesis, and must not inherit the
161+
// seconds of audio the first session never finished delivering.
162+
expect(second.samples).toEqual(new Set([HEALTHY_SESSION_SAMPLE]));
163+
expect(second.frames).toBeLessThan(BACKLOG_FRAMES);
164+
expect(gateway.connections).toBe(2);
165+
166+
await tts.close();
167+
}, 20_000);
168+
});

0 commit comments

Comments
 (0)