Skip to content

fix(rtc): don't block subscribe and unsubscribe while FfiQueue delivers - #892

Open
swayamg20 wants to merge 1 commit into
livekit:mainfrom
swayamg20:fix/888-ffiqueue-lock
Open

swayamg20 wants to merge 1 commit into
livekit:mainfrom
swayamg20:fix/888-ffiqueue-lock

Conversation

@swayamg20

Copy link
Copy Markdown
Contributor

Summary

FfiQueue.put held _lock for the whole delivery, so subscribe and unsubscribe, which every FFI request calls from its event loop, waited until the FFI callback thread had run every filter and call_soon_threadsafe.

  • subscribe and unsubscribe replace the subscriber list instead of mutating it, so put iterates without _lock, as suggested in the issue
  • A separate _put_lock keeps concurrent puts serialized, so every subscriber still sees events in the same order

Closes #888

Testing

  • uv run pytest tests/rtc/test_ffi_queue.py tests/rtc/test_room_ffi_filter.py -q -W error
  • uv run mypy livekit-protocol livekit-api livekit-rtc
  • uv run ruff check . and uv run ruff format --check .

Scope

unsubscribe no longer waits for an in-flight put, so that put can still deliver its item to the removed queue.

@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: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@github-actions

Copy link
Copy Markdown
Contributor

👋 This PR has no changeset. If it should be released, add a markdown file under .changeset/ (any name, e.g. .changeset/fix-token-parsing.md):

---
"livekit-rtc": patch
---

Short description of the change, used in the release notes.
  • Packages: livekit-rtc, livekit-api, livekit-protocol (one line per package)
  • Types: patch, minor, major, patch-rc, minor-rc, major-rc, next-rc, promote

If this change doesn't need a release (docs, CI, tests), you can ignore this comment.

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

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:

  1. Add a changeset, e.g. .changeset/ffi-queue-nonblocking-subscribe.md with "livekit-rtc": patch.
  2. Don't log a closed loop as an error. Since unsubscribe() no longer waits for a put in 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_threadsafe raises and we log error putting to queue: Event loop is closed at ERROR level. I could reproduce this locally. Skipping subscribers whose loop.is_closed() in put (or logging that case at debug) avoids the noise. A short comment that unsubscribe() 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.

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.

FfiQueue.put holds the subscriber lock across call_soon_threadsafe, so subscribe/unsubscribe on other event loops wait for the whole delivery

2 participants