fix(inference): keep a reference to the STT session.update task - #7053
Closed
Rehansanjay wants to merge 1 commit into
Closed
fix(inference): keep a reference to the STT session.update task#7053Rehansanjay wants to merge 1 commit into
Rehansanjay wants to merge 1 commit into
Conversation
`SpeechStream.update_options` applies the new model, language or extras to `self._opts` and then fires `asyncio.ensure_future` to tell the server about them. The event loop only holds a weak reference to that future, so it can be garbage collected before the message is sent. When that happens the failure is silent and one-sided: local options say the model changed, the server was never told, and the stream keeps transcribing with the old settings. Nothing raises and nothing logs. Hold the task in a set that discards on completion, and cancel whatever is still pending when the run loop tears the websocket down, next to the existing `gracefully_cancel` of the send/recv/vad tasks. Caught by ruff's RUF006 (asyncio-dangling-task), which is not currently enabled in this repo.
Contributor
Author
|
Bumping this one because it is the only finding from the sweep that is in core rather than a plugin, and its failure mode is silent.
Same class as #7023 and #7012, both merged. One file, +9/-1, all checks green, no open review threads. cc @tinalenguyen since you merged #7012 and have been the most recent hand in |
Contributor
Author
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.
The bug
SpeechStream.update_optionsupdatesself._optsand then tells the server about it:The event loop holds only a weak reference to that future, so it can be garbage collected before the message is ever sent.
The failure is silent and one-sided. Local options say the model, language or extras changed; the server was never told; the stream keeps transcribing with the old settings. Nothing raises, nothing logs, and the caller has no way to notice.
The pending future is also never cancelled when the run loop tears the websocket down, so it can outlive the socket it was meant to write to.
The fix
Hold the task in a set that discards itself on completion, and cancel anything still pending in the run loop's
finally, right next to thegracefully_cancelalready there for the send/recv/vad tasks.No behaviour change on the success path — the update is still sent in the background and
update_optionsstill returns immediately.How it was found
ruff --select RUF006(asyncio-dangling-task). The repo's[tool.ruff.lint] selectis currentlyE, W, F, I, B, C4, UP, soRUFis not enabled and this rule never runs in CI.Across
livekit-agentsandlivekit-pluginsthe rule currently reports 8 findings. This PR is the only one in core; the other seven are covered by #7050 (inworld), #7051 (soniox) and #7052 (aws), plus one inlivekit-plugins-hammingthat sits inside_reset_runtime_for_testsand is harmless.Worth noting my own AST scan missed this one because it only looked for
create_task—ensure_futurehas the same weak-reference semantics and ruff checks both.If it would be useful, I am happy to send a follow-up that fixes the hamming case and adds
"RUF"(or justRUF006) to the ruff select, so the whole class is caught in CI rather than one plugin at a time. Say the word and I will open it once these have landed.Checks
ruff check,ruff check --select RUF006andruff format --checkall pass on the changed file.