-
Notifications
You must be signed in to change notification settings - Fork 333
fix(inference): treat a dropped TTS gateway session as a failed attempt #2144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+487
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| '@livekit/agents': patch | ||
| --- | ||
|
|
||
| fix(inference): treat a dropped TTS gateway session as a failed attempt | ||
|
|
||
| When the inference gateway ends a TTS session with `session.closed` part-way through a | ||
| reply, the JS client treated it as a successful completion. That single mistake had two | ||
| consequences. The rest of the reply's text was discarded with no error, no warning and no | ||
| retry — in the trace this came from, a ~9000-character reply had only 2017 characters | ||
| submitted before the drop. And the gateway websocket went back into the `ConnectionPool` | ||
| while the gateway was still mid-synthesis, so the next reply picked it up and read the | ||
| previous reply's outstanding audio as its own: after one barge-in the following reply spoke | ||
| 53.8s of the previous answer while the transcript showed the new one. | ||
|
|
||
| The dropped session now flushes the audio it did produce, marks that segment's last frame | ||
| final, and rejects with a retryable `APIStatusError`, so the socket is evicted and the retry | ||
| finishes the reply. Socket reuse is additionally gated on having observed the gateway's | ||
| `done`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // SPDX-FileCopyrightText: 2026 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import type { AddressInfo } from 'node:net'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { WebSocketServer } from 'ws'; | ||
| import type { WebSocket as WsSocket } from 'ws'; | ||
| import { initializeLogger } from '../log.js'; | ||
| import { TTS } from './tts.js'; | ||
|
|
||
| /** | ||
| * Pins the user-visible invariant: a reply must never inherit the audio a previous, dropped | ||
| * session never finished delivering. | ||
| * | ||
| * `session.closed` is the only route that reaches that leak. Once the `session.closed` | ||
| * handler rejects the attempt instead of resolving it, the exception path in | ||
| * `ConnectionPool.withConnection` evicts the socket by itself and this test passes with or | ||
| * without the `sessionDrained` eviction in `SynthesizeStream.run`. Read it as coverage of | ||
| * the behaviour, not of that eviction. | ||
| */ | ||
|
|
||
| initializeLogger({ pretty: false }); | ||
|
|
||
| const SAMPLE_RATE = 16000; | ||
| const FRAME_MS = 20; | ||
| const SAMPLES_PER_FRAME = (SAMPLE_RATE * FRAME_MS) / 1000; | ||
|
|
||
| /** Audio from the session that gets dropped and audio from a healthy session carry | ||
| * distinct constant samples so the test can tell, per frame, which session's synthesis a | ||
| * frame actually came from. */ | ||
| const DROPPED_SESSION_SAMPLE = 1000; | ||
| const HEALTHY_SESSION_SAMPLE = 2000; | ||
|
|
||
| /** ~6s of already-synthesized audio the gateway still owes after it drops the session. */ | ||
| const BACKLOG_FRAMES = 300; | ||
| const OWN_FRAMES = 25; | ||
|
|
||
| function audioEvent(sessionId: string, sample: number): string { | ||
| const pcm = Buffer.alloc(SAMPLES_PER_FRAME * 2); | ||
| for (let i = 0; i < SAMPLES_PER_FRAME; i++) { | ||
| pcm.writeInt16LE(sample, i * 2); | ||
| } | ||
| return JSON.stringify({ | ||
| type: 'output_audio', | ||
| session_id: sessionId, | ||
| audio: pcm.toString('base64'), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Gateway stand-in for the production trace in which the first reply's session was dropped | ||
| * with `session.closed` while ~90s of synthesis was still outstanding. On the first | ||
| * connection it streams a short prefix, drops the session without a `done`, then keeps | ||
| * flushing the rest of the backlog onto the same socket. Any later connection behaves | ||
| * normally. | ||
| */ | ||
| async function startFakeGateway() { | ||
| const wss = new WebSocketServer({ port: 0, host: '127.0.0.1' }); | ||
| await new Promise<void>((resolve) => wss.once('listening', () => resolve())); | ||
| let connections = 0; | ||
| const sockets: WsSocket[] = []; | ||
|
|
||
| wss.on('connection', (ws: WsSocket) => { | ||
| sockets.push(ws); | ||
| const index = ++connections; | ||
| const sessionId = `session-${index}`; | ||
| const sample = index === 1 ? DROPPED_SESSION_SAMPLE : HEALTHY_SESSION_SAMPLE; | ||
| let dropped = false; | ||
|
|
||
| const send = (payload: string) => { | ||
| if (ws.readyState === ws.OPEN) ws.send(payload); | ||
| }; | ||
|
|
||
| ws.on('message', async (raw: Buffer) => { | ||
| const event = JSON.parse(raw.toString()) as { type: string }; | ||
| if (event.type === 'session.create') { | ||
| send(JSON.stringify({ type: 'session.created', session_id: sessionId })); | ||
| return; | ||
| } | ||
| if (event.type !== 'session.flush') return; | ||
|
|
||
| // A healthy session serves every reply it is asked for, so a pooled socket can be | ||
| // reused across replies. | ||
| if (index > 1) { | ||
| for (let i = 0; i < OWN_FRAMES; i++) send(audioEvent(sessionId, sample)); | ||
| send(JSON.stringify({ type: 'done', session_id: sessionId })); | ||
| return; | ||
| } | ||
|
|
||
| if (dropped) return; | ||
| dropped = true; | ||
|
|
||
| // Reply 1: hand over a short prefix, then drop the session mid-synthesis. | ||
| for (let i = 0; i < OWN_FRAMES; i++) send(audioEvent(sessionId, sample)); | ||
| send(JSON.stringify({ type: 'session.closed', session_id: sessionId })); | ||
| // The synthesis that was already in flight keeps arriving on this socket. | ||
| await new Promise((resolve) => setTimeout(resolve, 20)); | ||
| for (let i = 0; i < BACKLOG_FRAMES; i++) send(audioEvent(sessionId, sample)); | ||
| send(JSON.stringify({ type: 'done', session_id: sessionId })); | ||
| }); | ||
| }); | ||
|
|
||
| const { port } = wss.address() as AddressInfo; | ||
| return { | ||
| baseURL: `http://127.0.0.1:${port}/v1`, | ||
| get connections() { | ||
| return connections; | ||
| }, | ||
| close: () => { | ||
| for (const socket of sockets) socket.terminate(); | ||
| return new Promise<void>((resolve) => wss.close(() => resolve())); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async function synthesize(tts: TTS<string>, text: string) { | ||
| const stream = tts.stream(); | ||
| stream.pushText(text); | ||
| stream.endInput(); | ||
|
|
||
| const samples = new Set<number>(); | ||
| let frames = 0; | ||
| for await (const event of stream) { | ||
| if (typeof event === 'symbol' || event.frame.samplesPerChannel === 0) continue; | ||
| frames++; | ||
| samples.add(event.frame.data[0]!); | ||
| } | ||
| await stream.close(); | ||
| return { frames, samples }; | ||
| } | ||
|
|
||
| describe('inference TTS pooled socket reuse', () => { | ||
| let gateway: Awaited<ReturnType<typeof startFakeGateway>>; | ||
|
|
||
| beforeEach(async () => { | ||
| gateway = await startFakeGateway(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await gateway.close(); | ||
| }); | ||
|
|
||
| it('does not hand a dropped session\u2019s outstanding audio to the next reply', async () => { | ||
| const tts = new TTS({ | ||
| model: 'inworld/inworld-tts-2', | ||
| voice: 'Sarah', | ||
| sampleRate: SAMPLE_RATE, | ||
| baseURL: gateway.baseURL, | ||
| apiKey: 'devkey', | ||
| apiSecret: 'secret'.padEnd(32, 'x'), | ||
| }); | ||
|
|
||
| // The dropped session fails the attempt, so the first reply is the prefix it did | ||
| // deliver plus the audio from the retry that finishes it on a healthy session. | ||
| const first = await synthesize(tts, 'Tell me a long story about the lighthouse.'); | ||
| expect(first.samples).toEqual(new Set([DROPPED_SESSION_SAMPLE, HEALTHY_SESSION_SAMPLE])); | ||
|
|
||
| const second = await synthesize(tts, 'Tell me a long joke about skeletons.'); | ||
|
|
||
| // The second reply must speak only its own synthesis, and must not inherit the | ||
| // seconds of audio the first session never finished delivering. | ||
| expect(second.samples).toEqual(new Set([HEALTHY_SESSION_SAMPLE])); | ||
| expect(second.frames).toBeLessThan(BACKLOG_FRAMES); | ||
| expect(gateway.connections).toBe(2); | ||
|
|
||
| await tts.close(); | ||
| }, 20_000); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why would the gateway send
session.closedin the middle of the session?