-
Notifications
You must be signed in to change notification settings - Fork 201
Fix pending tracks race condition #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
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
Closed
rokk4
added a commit
to rokk4/client-sdk-flutter
that referenced
this pull request
Dec 2, 2025
Fixes race condition where tracks arriving before participant metadata were permanently dropped from the pending queue after timeout, causing 10-60 second delays or complete failures when participants rejoin. Changes: 1. Retry transient failures: Modified _flushPendingTracks() to differentiate between transient (notTrackMetadataFound) and permanent failures. Transient failures now keep tracks in queue for retry instead of removing them. 2. Additional flush trigger: Added listener to flush pending tracks when SignalParticipantUpdateEvent contains track publications, ensuring tracks are subscribed once metadata becomes available. 3. Improved logging: Transient failures logged at fine level to reduce noise, permanent failures at severe level for visibility. The fix maintains the existing timeout configuration from connectOptions while enabling retry logic that resolves the race condition where: - WebRTC track arrives first → queued - ParticipantInfo arrives → participant created → flush fails (no publications) - TrackPublishedResponse arrives later → second flush succeeds This reduces track subscription latency after rejoin from 10-60s to <1s and improves reliability on slower devices where the race condition was more pronounced. Related: livekit#928
rokk4
added a commit
to rokk4/client-sdk-flutter
that referenced
this pull request
Dec 2, 2025
… logic Combines defensive and reactive approaches to fix race condition where tracks arriving before participant metadata caused 10-60s delays or failures on rejoin. Root Cause: When a participant rejoins, WebRTC tracks can arrive before signaling metadata. The previous logic had three critical gaps: 1. Tracks queued but dropped on timeout (no retry) 2. Missing flush triggers when metadata finally arrives 3. Insufficient deferral check (only participant existence, not publication) Solution - Three-Layer Defense: 1. PREVENTIVE: Enhanced deferral logic (NEW) Check not just participant existence, but also publication metadata: - connectionState != connected (pre-connection tracks) - participant == null (tracks before participant) - publication == null (tracks before metadata) ← NEW CHECK This prevents premature subscription attempts that would timeout. 2. REACTIVE: Retry transient failures Modified _flushPendingTracks() to differentiate failure types: - notTrackMetadataFound → return false (keep in queue, retry) - Other failures → return true (remove from queue) Handles micro-timing races where flush happens before metadata processed. 3. AGGRESSIVE: Additional flush trigger Added SignalParticipantUpdateEvent listener to flush when track publications arrive, ensuring queued tracks are processed promptly. Impact: - Reduces rejoin latency from 10-60s to <1s - Eliminates frozen frames on rejoin - More robust on slower devices (reduced CPU-dependent timing sensitivity) - Maintains configurable timeout from connectOptions The combined approach is superior because: - Prevention reduces unnecessary timeout waits - Retry ensures recovery from edge cases - Aggressive flush ensures timely processing - Event-driven design scales better than polling Related: livekit#928
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.
No description provided.