fix: handle race between LocalTrackSubscribed signal and publishTrack completion#1872
Open
pabloFuente wants to merge 1 commit intolivekit:mainfrom
Open
fix: handle race between LocalTrackSubscribed signal and publishTrack completion#1872pabloFuente wants to merge 1 commit intolivekit:mainfrom
LocalTrackSubscribed signal and publishTrack completion#1872pabloFuente wants to merge 1 commit intolivekit:mainfrom
Conversation
Handle race between `LocalTrackSubscribed` signal and `publishTrack` completion.
|
Contributor
Author
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
When a local participant publishes a track, there is a race condition between two asynchronous flows:
EngineEvent.LocalTrackSubscribedwith the track SID.LocalParticipant.publishTrack()finishes its async work and callsaddTrackPublication()to register theLocalTrackPublication.If the signaling confirmation arrives before
addTrackPublication()completes, theLocalTrackSubscribedhandler in Room.ts callsgetTrackPublications().find(...), finds nothing, logs a warning ("could not find local track subscription for subscribed event"), and silently drops the event. NeitherParticipantEvent.LocalTrackSubscribednorRoomEvent.LocalTrackSubscribedis ever emitted for that track because the warning log statement will just return without defering the event:I have been able to replicate this behavior consistently in a high concurrency scenario, where many users connect, publish and subscribe multiple tracks to the same Room at the same time.
Fix
This PR replaces the inline handler with
handleLocalTrackSubscribed(), which uses a deferred-emit pattern. That is consistent with the existingonTrackAddeddefer logic to already present in Room.ts, that helps handling it when the participant is inConnectingorReconnectingstate (see here).My proposal for the defering logic of the LocalTrackSubscribed event follows this path:
ParticipantEvent.LocalTrackPublishedon the local participant. When the matchingtrackSidappears, emit theLocalTrackSubscribedevents.A small
emitLocalTrackSubscribed()helper is extracted to deduplicate the two-event emission (ParticipantEvent+RoomEvent).