Skip to content

fix(avatar): handle a failed stream close in DataStreamAudioOutput.flush() - #8030

Open
regalmoix wants to merge 2 commits into
livekit:mainfrom
regalmoix:fix/avatar-flush-close-error
Open

regalmoix wants to merge 2 commits into
livekit:mainfrom
regalmoix:fix/avatar-flush-close-error

Conversation

@regalmoix

Copy link
Copy Markdown
Contributor

flush() closes the segment's byte stream in a fire-and-forget task and never reads its result. When the room disconnects mid-reply (e.g. the user leaves), the close fails and asyncio logs Task exception was never retrieved with a traceback at teardown:

  • room.disconnect() drops the participant's open writer handles, so the close request hits a dropped handle and the FFI client raises AssertionError.
  • If the close races the disconnect, the callback carries an error and aclose() raises StreamError.

Fix

aclose() now runs in a small coroutine that logs a failure instead of leaving it unretrieved. It logs at debug when the room is already disconnected, the expected teardown case with nothing to recover. It logs at warning if the room is still connected, because then the avatar never got the end of the segment. This matches the transcription writer close in room_io/_output.py.

Testing

  • New test_flush_close_failure_is_handled: the writer's aclose() raises. The test asserts that the flush task completes and that the warning is logged. On main it fails with the raw StreamError.
  • tests/test_datastream_audio_output.py, tests/test_audio_sink_proxy.py and tests/test_plugin_keyframe.py pass. ruff is clean, and make type-check reports no new errors.

Why no regression

  • Only the failure path changes. A successful close behaves as before, and the task is still tracked in _tasks.
  • KeyframeAudioOutput already has its own _close_stream, so the new helper has a different name and Keyframe is untouched.
  • Keyframe's close task has the same unretrieved-error pattern. I'm happy to fix it in a follow-up.

@regalmoix
regalmoix requested a review from a team as a code owner October 21, 2026 14:08
@regalmoix

Copy link
Copy Markdown
Contributor Author

@longcw @chenghao-mou — small follow-up to #8028 territory (same file, independent). Would you mind taking a look when you have a moment?

@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 found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…ush()

flush() closed the segment's byte stream in a fire-and-forget task and
never read its result, so a close failing after the room disconnected
surfaced as "Task exception was never retrieved". Await the close in a
small coroutine and log the failure: debug once the room is gone,
warning while it is still connected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@regalmoix
regalmoix force-pushed the fix/avatar-flush-close-error branch from f366369 to bb7db5e Compare October 21, 2026 14:16
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@regalmoix

Copy link
Copy Markdown
Contributor Author

Closing to retrigger CI — unit-tests hit the same unrelated test_google_long_decode.py leaked-task flake that #8023 saw. Reopening.

@regalmoix regalmoix closed this Oct 21, 2026
@regalmoix regalmoix reopened this Oct 21, 2026
@tanish111

Copy link
Copy Markdown
Contributor

@karlsonlee-livekit If you could recall we discussed about this types of failure and better end call handling. Does this fix the issue?

This branch has not been deployed

No deployments
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.

3 participants