Fix race condition in PeerConnectionTransport.trackBitrates#821
Open
adrian-niculescu wants to merge 1 commit intolivekit:mainfrom
Open
Fix race condition in PeerConnectionTransport.trackBitrates#821adrian-niculescu wants to merge 1 commit intolivekit:mainfrom
adrian-niculescu wants to merge 1 commit intolivekit:mainfrom
Conversation
🦋 Changeset detectedLatest commit: e16141d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Problem
PeerConnectionTransport.trackBitratesis a plainmutableMapOf<...>()(aLinkedHashMap), with no thread-safety guarantees. Reads and writes race across threads inside a singleLocalParticipant.negotiate()call:Auto-renegotiate (read on RTC thread):
engine.createSenderTransceiver(...)causes WebRTC to fireonRenegotiationNeeded→PublisherTransportObserver.onRenegotiationNeeded()→engine.negotiatePublisher()→ the 20ms-debouncednegotiatefield onPeerConnectionTransport. When the debounce closes,createAndSendOfferruns on the RTC thread and iterates the map viaensureCodecBitrates(...).Direct write (off RTC thread): still in the same
negotiate()body,engine.registerTrackBitrateInfo(...)writes to the map on the calling coroutine's thread.The auto-trigger is intentional and explicitly documented in
LocalParticipant("PublisherTransportObserver.onRenegotiationNeeded()gets triggered automatically so no need to call negotiate manually."). Whenever the debounce closes before the write completes, the read on the RTC thread overlaps an in-flight write —ConcurrentModificationExceptioninensureCodecBitrates, or a missing/inconsistent bitrate hint in the outgoing offer SDP. The race is intrinsic to thiscreateSenderTransceiver→registerTrackBitrateInfosequence and exists on every publish path that exercises it (SVC video with amaxBitrateBpsencoding); the fast-publishcoroutineScopeis one such path, not the cause.Fix
Wrap both
registerTrackBitrateInfooverloads inexecuteRTCIfNotClosed { ... }so writes are serialized onto the same single-threaded RTC executor that already serves the read site. This matches the existing pattern used byupdateRTCConfigandaddIceCandidatein the same class — the RTC thread is effectively the lock for peer-connection-adjacent state.The closed-state guard inside
executeRTCIfNotClosedalso givesregisterTrackBitrateInfoconsistent post-close semantics (silently dropped) with the surrounding APIs.Scope
MutexaroundcreateAndSendOfferfor concurrent offer creation). That PR did not touchregisterTrackBitrateInfo; this race remains onmain.