Fix/reconcile data track subscription deadlock (#4843)
* fixed deadlock in reconcileDataTrackSubscription * test * review changes
This commit is contained in:
@@ -599,7 +599,7 @@ func (m *SubscriptionManager) reconcileDataTrackSubscriptions() {
|
||||
var needsToReconcile []*dataTrackSubscription
|
||||
m.lock.RLock()
|
||||
for _, sub := range m.dataTrackSubscriptions {
|
||||
if sub.needsSubscribe() || sub.needsUnsubscribe() {
|
||||
if sub.needsSubscribe() || sub.needsUnsubscribe() || sub.needsCleanup() {
|
||||
needsToReconcile = append(needsToReconcile, sub)
|
||||
}
|
||||
}
|
||||
@@ -630,7 +630,7 @@ func (m *SubscriptionManager) reconcileDataTrackSubscription(s *dataTrackSubscri
|
||||
if s.durationSinceStart() > notFoundTimeout {
|
||||
s.logger.Infow("unsubscribing from data track after notFoundTimeout", "error", err)
|
||||
s.setDesired(false)
|
||||
m.queueReconcile(s.trackID)
|
||||
m.queueReconcileDataTrack(s.trackID)
|
||||
}
|
||||
default:
|
||||
// all other errors
|
||||
@@ -668,12 +668,16 @@ func (m *SubscriptionManager) reconcileDataTrackSubscription(s *dataTrackSubscri
|
||||
}
|
||||
|
||||
m.lock.Lock()
|
||||
if s.needsCleanup() {
|
||||
cleanedUp := s.needsCleanup()
|
||||
if cleanedUp {
|
||||
s.logger.Debugw("cleanup removing data track subscription")
|
||||
delete(m.dataTrackSubscriptions, s.trackID)
|
||||
m.notifyDataTrackSubscriberHandles()
|
||||
}
|
||||
m.lock.Unlock()
|
||||
|
||||
if cleanedUp {
|
||||
m.notifyDataTrackSubscriberHandles()
|
||||
}
|
||||
}
|
||||
|
||||
// trigger an immediate reconciliation, when trackID is empty, will reconcile all subscriptions
|
||||
|
||||
@@ -526,6 +526,33 @@ func TestSubscribeDataTrack(t *testing.T) {
|
||||
}, subSettleTimeout, subCheckInterval, "should be resubscribed")
|
||||
require.Equal(t, 2, resolver.dataTrack.AddSubscriberCallCount())
|
||||
})
|
||||
|
||||
t.Run("unsubscribe before data track resolves", func(t *testing.T) {
|
||||
sm := newTestSubscriptionManager()
|
||||
defer sm.Close(false)
|
||||
// no track available, subscribe attempts fail with ErrTrackNotFound
|
||||
resolver := newTestDataTrackResolver(true, false, "pub", "pubID")
|
||||
sm.params.DataTrackResolver = resolver.Resolve
|
||||
|
||||
sm.SubscribeToDataTrack("track")
|
||||
sm.lock.RLock()
|
||||
s := sm.dataTrackSubscriptions["track"]
|
||||
sm.lock.RUnlock()
|
||||
require.NotNil(t, s)
|
||||
|
||||
// let the worker attempt (and fail) the subscribe
|
||||
require.Eventually(t, func() bool {
|
||||
return s.getNumAttempts() > 0
|
||||
}, subSettleTimeout, subCheckInterval, "no subscribe attempt was made")
|
||||
|
||||
sm.UnsubscribeFromDataTrack("track")
|
||||
require.Eventually(t, func() bool {
|
||||
sm.lock.RLock()
|
||||
_, ok := sm.dataTrackSubscriptions["track"]
|
||||
sm.lock.RUnlock()
|
||||
return !ok
|
||||
}, subSettleTimeout, subCheckInterval, "data track subscription was not cleaned up")
|
||||
})
|
||||
}
|
||||
|
||||
type testSubscriptionParams struct {
|
||||
|
||||
Reference in New Issue
Block a user