fix(voice): don't duplicate queued speeches on scheduling tie - #7283
Open
yaosongding wants to merge 1 commit into
Open
yaosongding wants to merge 1 commit into
yaosongding wants to merge 1 commit into
Conversation
`_schedule_speech` pushed `(-priority, time.perf_counter_ns(), speech)` into a max-heap and swallowed `TypeError` to retry when timestamps tied. Because `heapq.heappush` appends the item before sifting, catching `TypeError` from the unorderable `SpeechHandle` leaves the failed item in the list. Each retry appends another copy and breaks the heap invariant, causing duplicate generation and playout tasks. Break ties with a strictly increasing counter (`itertools.count()`) instead. `(priority, seq)` never ties, eliminating the `TypeError` and the retry loop while guaranteeing strict FIFO order for equal priorities.
yaosongding
marked this pull request as ready for review
September 15, 2026 04:53
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.
Problem:
AgentActivity._schedule_speechpushes(-priority, time.perf_counter_ns(), speech)ontoself._speech_qin awhile Trueloop that catchesTypeError. When priority and timestamp tie, Python compares the unorderableSpeechHandleand raisesTypeError. Becauseheapq.heappushappends the item to the underlying list before sifting up, catchingTypeErrorand retrying leaves the first item in the queue and appends a duplicate, breaking the heap invariant and scheduling duplicate playout tasks.Fix: Break ties using a strictly increasing counter (
itertools.count()) instead of timestamps. Since(priority, seq)never ties, comparison never reachesSpeechHandle, eliminating theTypeErrorand retry loop while guaranteeing strict FIFO order for equal-priority speeches.Verification
tests/test_speech_queue.pyverifying same-priority speeches are enqueued without duplicates.pytest tests/test_speech_queue.py tests/test_interrupt_protected_speech.py --unit -q.ruff checkandmypy -p livekit.agents.