Cache transceiver before closing subscribed track. (#2594)

On migration, when subscription moved from remote -> local,
transceiver caching was racing. Although a very small possibility,
it could happen like so

1. down track close
2. down track close callback fires go routine to close subscribed track
3. subscribed track close handler in subscription manager tries to
   reconcile
4. reconcile adds subscribed track again
5. cannot find cached transceiver as caching happens after down track
   close finishes in stap 1 above. Although there are a couple of
   gortouine jumps (step 2 fires a goroutine to close subscribed track
   and step 4 will reconcile in a goroutine too), it is theoretically
   possible that the step 1 has not finished and hence transceiver is
   not cached.

Fix is to move caching to before closing subscribed track.
This commit is contained in:
Raja Subramanian
2024-03-22 11:56:50 +05:30
committed by GitHub
parent 0f597e7e46
commit ffb831aa8c
4 changed files with 40 additions and 25 deletions
+7 -1
View File
@@ -241,10 +241,16 @@ func (s *StreamAllocator) AddTrack(downTrack *sfu.DownTrack, params AddTrackPara
track := NewTrack(downTrack, params.Source, params.IsSimulcast, params.PublisherID, s.params.Logger)
track.SetPriority(params.Priority)
trackID := livekit.TrackID(downTrack.ID())
s.videoTracksMu.Lock()
s.videoTracks[livekit.TrackID(downTrack.ID())] = track
oldTrack := s.videoTracks[trackID]
s.videoTracks[trackID] = track
s.videoTracksMu.Unlock()
if oldTrack != nil {
oldTrack.DownTrack().SetStreamAllocatorListener(nil)
}
downTrack.SetStreamAllocatorListener(s)
if s.prober.IsRunning() {
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate