Skip to content

Start AudioStream _run task after the FFI handle is assigned#742

Open
apoorva-01 wants to merge 1 commit into
livekit:mainfrom
apoorva-01:fix/audiostream-orphaned-run-task
Open

Start AudioStream _run task after the FFI handle is assigned#742
apoorva-01 wants to merge 1 commit into
livekit:mainfrom
apoorva-01:fix/audiostream-orphaned-run-task

Conversation

@apoorva-01

Copy link
Copy Markdown

AudioStream.__init__ kicks off the _run task before it creates the stream and sets self._ffi_handle. So if stream creation throws (e.g. the track got torn down when the participant dropped mid-setup), __init__ raises like it should, but the _run task is already running and trips over the unset self._ffi_handle with AttributeError: 'AudioStream' object has no attribute '_ffi_handle'. It leaks as an unretrieved task exception on the livekit logger, so the caller can't catch it.

Fix is just to start the task after the handle is set. Nothing awaits in between so the order doesn't matter for correctness, and it lines up with VideoStream, which already does it this way.

Could also default _ffi_handle to None and guard _is_event, but that leaves a _run task spinning on a predicate that never matches, so I went with the reorder. Happy to switch if you'd rather. Test fails on the old order, passes now.

closes #729

If owned-stream creation raises, __init__ never assigns _ffi_handle, but the
already-scheduled _run task still dereferences it and leaks an uncatchable
AttributeError on the livekit logger. Creating the task last leaves no orphan
on that path.
@CLAassistant

CLAassistant commented Jul 3, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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.

Open in Devin Review

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.

🚩 Test may not be discovered by root pytest configuration

The root pyproject.toml sets testpaths = ["tests"] which points to the repo-root tests/ directory. The new test file lives in livekit-rtc/tests/, which is a separate directory. Unless pytest is invoked from within livekit-rtc/ or with an explicit path, this test may not be collected by the default pytest invocation from the repo root. Other tests in livekit-rtc/tests/ (e.g., test_audio.py) appear to be integration/E2E tests that may be run separately, so this might be intentional — but worth confirming the CI configuration discovers it.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good flag, but CI does collect it. The test jobs run pytest tests/ livekit-rtc/tests/ with both paths explicit (tests.yml), which overrides the testpaths = ["tests"] default. It shows up green in this PR's run, e.g. the ubuntu-3.9 job: livekit-rtc/tests/test_audio_stream_init.py ..

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.

AudioStream: orphaned _run task raises AttributeError: '_ffi_handle' when owned-stream creation fails in __init__

2 participants