Skip to content

fix(elevenlabs): stop routing TTS audio to an interrupted stream - #6936

Open
longcw wants to merge 1 commit into
mainfrom
longc/elevenlabs-late-audio
Open

fix(elevenlabs): stop routing TTS audio to an interrupted stream#6936
longcw wants to merge 1 commit into
mainfrom
longc/elevenlabs-late-audio

Conversation

@longcw

@longcw longcw commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Problem

The ElevenLabs plugin sends many TTS streams over one websocket, and its recv loop routes each message through a context map owned by the connection.
An interrupted run ends its audio segment, but it leaves the context in that map.
The server still sends the audio that it generated before the close, so those late bytes reach an emitter with no open segment, and AudioEmitter._main_task stops with RuntimeError: start_segment() must be called before pushing audio data.
Fixes #6929.

Fix

_Connection.unregister_stream() drops the routing entry for a stream, and SynthesizeStream._run calls it before it ends the segment.
The context id stays in _active_contexts, so close_context() still asks the server to release the context (#5844).
A message for a context that is no longer registered is now normal, so the recv loop logs it at debug level.
A final message still frees the context id, and the drain of an old connection continues to work.

The recv loop routes messages through a context map owned by the shared
websocket connection. An interrupted run ended its audio segment but left
the context registered, so the audio the server flushes after
close_context() was pushed into an emitter with no open segment, killing
AudioEmitter._main_task with "start_segment() must be called before
pushing audio data".

Unregister the stream before ending the segment. The context id stays in
_active_contexts so close_context() still releases it server-side.
@longcw
longcw requested a review from a team as a code owner August 21, 2026 03:17

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ElevenLabs cancellation can push late audio after end_segment(), crashing AudioEmitter

1 participant