fix(cartesia): ignore stale frames from previous contexts on pooled websockets - #7081
Merged
chenghao-mou merged 1 commit intoSep 1, 2026
Merged
Conversation
…ebsockets When the ConnectionPool returns a previously-used websocket, the socket buffer may still contain unread audio frames / done from the abandoned context (occurring when a SynthesizeStream was interrupted before the frames were fully consumed). The _recv_task did not validate the context_id on received messages, so stale audio was pushed into the new output and a stale "done" could prematurely truncate the new synthesis. - Add context_id check in _recv_task: skip any message whose context_id is present but does not match the current cartesia_context_id. Messages without a context_id (e.g. server errors) are not affected. - The current segment is only started from the first matching message. The unit test simulates a reused socket where stale (wrong context_id) audio + done arrive before the legitimate frames and asserts they are silently dropped.
Panmax
force-pushed
the
fix/cartesia-stale-context-frames
branch
from
September 1, 2026 09:46
af7fc26 to
56dadab
Compare
chenghao-mou
approved these changes
Sep 1, 2026
chenghao-mou
left a comment
Member
There was a problem hiding this comment.
lgtm. Thanks for the PR!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When the ConnectionPool returns a previously-used websocket, the socket buffer may still contain unread audio frames / done from the abandoned context (occurring when a SynthesizeStream was interrupted before the frames were fully consumed). The
_recv_taskdid not validate thecontext_idon received messages, so stale audio was pushed into the new output and a stale "done" could prematurely truncate the new synthesis.Changes
_recv_task): skip any message whosecontext_idis present but does not match the currentcartesia_context_id. Messages without acontext_id(e.g. server errors) are not affected.Reproduction
Without the fix, sending a stale-context audio frame + done then current-context audio + done:
With the fix:
Related
Root cause analysis: In the production session, 27 consecutive
say()calls hadttfb≈0.002s(impossible over a fresh connection) andduration≈0.01s, confirming the socket was replaying stale frames instead of performing fresh synthesis. The"..."SPEAK text that triggered the visible symptom was just the most obvious case — any text sent through a pooled socket after an interrupted stream would exhibit the same corruption.