Fix race condition with unsubscribing from a republished track (#1429)
This commit is contained in:
@@ -38,7 +38,8 @@ var (
|
|||||||
// ensuring this is longer than iceFailedTimeout so we are certain the participant won't return
|
// ensuring this is longer than iceFailedTimeout so we are certain the participant won't return
|
||||||
notFoundTimeout = iceFailedTimeout
|
notFoundTimeout = iceFailedTimeout
|
||||||
// amount of time to try otherwise before flagging subscription as failed
|
// amount of time to try otherwise before flagging subscription as failed
|
||||||
subscriptionTimeout = iceFailedTimeout
|
subscriptionTimeout = iceFailedTimeout
|
||||||
|
trackRemoveGracePeriod = time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubscriptionManagerParams struct {
|
type SubscriptionManagerParams struct {
|
||||||
@@ -405,9 +406,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error {
|
|||||||
// do not unsubscribe, track is still available
|
// do not unsubscribe, track is still available
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// source track removed, we would unsubscribe
|
s.handleSourceTrackRemoved()
|
||||||
s.logger.Debugw("unsubscribing track since source track was removed")
|
|
||||||
s.setDesired(false)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -610,6 +609,8 @@ func (s *trackSubscription) getPublisherID() livekit.ParticipantID {
|
|||||||
|
|
||||||
func (s *trackSubscription) setDesired(desired bool) bool {
|
func (s *trackSubscription) setDesired(desired bool) bool {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
if desired {
|
if desired {
|
||||||
// as long as user explicitly set it to desired
|
// as long as user explicitly set it to desired
|
||||||
// we'll reset the timer so it has sufficient time to reconcile
|
// we'll reset the timer so it has sufficient time to reconcile
|
||||||
@@ -618,19 +619,17 @@ func (s *trackSubscription) setDesired(desired bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.desired == desired {
|
if s.desired == desired {
|
||||||
s.lock.Unlock()
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
s.desired = desired
|
s.desired = desired
|
||||||
s.lock.Unlock()
|
|
||||||
|
|
||||||
// when no longer desired, we no longer care about change notifications
|
// when no longer desired, we no longer care about change notifications
|
||||||
if desired {
|
if desired {
|
||||||
// reset attempts
|
// reset attempts
|
||||||
s.numAttempts.Store(0)
|
s.numAttempts.Store(0)
|
||||||
} else {
|
} else {
|
||||||
s.setChangedNotifier(nil)
|
s.setChangedNotifierLocked(nil)
|
||||||
s.setRemovedNotifier(nil)
|
s.setRemovedNotifierLocked(nil)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -683,34 +682,41 @@ func (s *trackSubscription) getSubscribedTrack() types.SubscribedTrack {
|
|||||||
|
|
||||||
func (s *trackSubscription) setChangedNotifier(notifier types.ChangeNotifier) bool {
|
func (s *trackSubscription) setChangedNotifier(notifier types.ChangeNotifier) bool {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
return s.setChangedNotifierLocked(notifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *trackSubscription) setChangedNotifierLocked(notifier types.ChangeNotifier) bool {
|
||||||
if s.changedNotifier == notifier {
|
if s.changedNotifier == notifier {
|
||||||
s.lock.Unlock()
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
existing := s.changedNotifier
|
existing := s.changedNotifier
|
||||||
s.changedNotifier = notifier
|
s.changedNotifier = notifier
|
||||||
s.lock.Unlock()
|
|
||||||
|
|
||||||
if existing != nil {
|
if existing != nil {
|
||||||
existing.RemoveObserver(string(s.subscriberID))
|
go existing.RemoveObserver(string(s.subscriberID))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *trackSubscription) setRemovedNotifier(notifier types.ChangeNotifier) bool {
|
func (s *trackSubscription) setRemovedNotifier(notifier types.ChangeNotifier) bool {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
return s.setRemovedNotifierLocked(notifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *trackSubscription) setRemovedNotifierLocked(notifier types.ChangeNotifier) bool {
|
||||||
if s.removedNotifier == notifier {
|
if s.removedNotifier == notifier {
|
||||||
s.lock.Unlock()
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
existing := s.removedNotifier
|
existing := s.removedNotifier
|
||||||
s.removedNotifier = notifier
|
s.removedNotifier = notifier
|
||||||
s.lock.Unlock()
|
|
||||||
|
|
||||||
if existing != nil {
|
if existing != nil {
|
||||||
existing.RemoveObserver(string(s.subscriberID))
|
go existing.RemoveObserver(string(s.subscriberID))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -751,6 +757,27 @@ func (s *trackSubscription) recordAttempt(success bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *trackSubscription) handleSourceTrackRemoved() {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
startedAt := s.subStartedAt.Load()
|
||||||
|
if startedAt == nil || time.Since(*startedAt) < trackRemoveGracePeriod {
|
||||||
|
// to prevent race conditions, if we've recently been asked to subscribe to a track
|
||||||
|
// ignore when source was removed. reconciler will take care of it eventually
|
||||||
|
// this would address the case when a track was unpublished and republished immediately
|
||||||
|
// it's possible for another caller to call setDesired(true) for the republished track before
|
||||||
|
// handleSourceTrackRemoved is called on the previously unpublished track
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// source track removed, we would unsubscribe
|
||||||
|
s.logger.Debugw("unsubscribing track since source track was removed")
|
||||||
|
s.desired = false
|
||||||
|
|
||||||
|
s.setChangedNotifierLocked(nil)
|
||||||
|
s.setRemovedNotifierLocked(nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *trackSubscription) maybeRecordError(ts telemetry.TelemetryService, pID livekit.ParticipantID, err error, isUserError bool) {
|
func (s *trackSubscription) maybeRecordError(ts telemetry.TelemetryService, pID livekit.ParticipantID, err error, isUserError bool) {
|
||||||
if s.eventSent.Swap(true) {
|
if s.eventSent.Swap(true) {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user