Repository navigation
Conversation
|
👋 This PR has no changeset. If it should be released, add a markdown file under ---
"livekit-rtc": patch
---
Short description of the change, used in the release notes.
If this change doesn't need a release (docs, CI, tests), you can ignore this comment. |
cloudwebrtc
left a comment
There was a problem hiding this comment.
Thanks, this is a good fix for #888! With copy-on-write subscribers, subscribe()/unsubscribe() no longer wait for a delivery in progress, and _put_lock still keeps puts ordered, so every subscriber sees events in the same order. It also fixes a reentrancy bug on main: an unsubscribe() from inside put on the FFI thread (e.g. from a filter, or a GC'd AudioStream.__del__) used to pop() from the list being iterated, so the next subscriber missed the event. Worth a line in the description. The new tests fail against main's FfiQueue and pass here. LGTM.
Two things to add before merging:
- Add a changeset, e.g.
.changeset/ffi-queue-nonblocking-subscribe.mdwith"livekit-rtc": patch. - Don't log a closed loop as an error. Since
unsubscribe()no longer waits for aputin progress, a put that took the old snapshot can still reach a subscriber that just unsubscribed. If that subscriber's loop has been closed in the meantime (e.g. a THREAD-executor job shutting down while the FFI thread is in a slow put),call_soon_threadsaferaises and we logerror putting to queue: Event loop is closedat ERROR level. I could reproduce this locally. Skipping subscribers whoseloop.is_closed()input(or logging that case at debug) avoids the noise. A short comment thatunsubscribe()no longer guarantees no further delivery would also help the next reader.
Nit, optional: _put_lock doesn't need to be reentrant unless a reentrant put is intended, so a plain threading.Lock would do.
The earlier failing Test (ubuntu-latest, Python 3.12) job was the shutdown SIGABRT flake (#968); the latest run is green.
Summary
FfiQueue.putheld_lockfor the whole delivery, sosubscribeandunsubscribe, which every FFI request calls from its event loop, waited until the FFI callback thread had run every filter andcall_soon_threadsafe.subscribeandunsubscribereplace the subscriber list instead of mutating it, soputiterates without_lock, as suggested in the issue_put_lockkeeps concurrent puts serialized, so every subscriber still sees events in the same orderCloses #888
Testing
uv run pytest tests/rtc/test_ffi_queue.py tests/rtc/test_room_ffi_filter.py -q -W erroruv run mypy livekit-protocol livekit-api livekit-rtcuv run ruff check .anduv run ruff format --check .Scope
unsubscribeno longer waits for an in-flightput, so that put can still deliver its item to the removed queue.