Repository navigation
Description
Describe the bug
SubscriptionManager.reconcileDataTrackSubscription calls
notifyDataTrackSubscriberHandles() while holding m.lock:
m.lock.Lock()
if s.needsCleanup() {
s.logger.Debugw("cleanup removing data track subscription")
delete(m.dataTrackSubscriptions, s.trackID)
m.notifyDataTrackSubscriberHandles()
}
m.lock.Unlock()notifyDataTrackSubscriberHandles() immediately does m.lock.Lock() again.
sync.RWMutex is not reentrant, so the reconcile worker deadlocks itself.
Each participant has a single reconcileWorker goroutine servicing both media
and data track subscriptions, so once this branch is hit:
- all subscription reconciliation (media + data) for that participant stops
permanently, and SubscriptionManager.Close()blocks forever waiting ondoneCh, hanging
participant teardown and leaking the associated resources.
There are two adjacent bugs in the same flow that also prevent cleanup from
working:
- In the
ErrTrackNotFoundbranch, afternotFoundTimeoutthe data track ID
is queued on the media reconcile channel (m.queueReconcile(s.trackID)
instead ofm.queueReconcileDataTrack(s.trackID)), so the intended
immediate cleanup never runs. - The periodic
reconcileDataTrackSubscriptions()only selects subscriptions
withneedsSubscribe() || needsUnsubscribe()— unlike its media counterpart
it omitsneedsCleanup(), so an abandoned data track subscription
(!desired && dataDownTrack == nil) is never reaped by the ticker either
and leaks indataTrackSubscriptions.
All three were introduced with data track subscriptions in Data tracks #4089.
Server
- Version: v1.13.6 / master (a8ce398)
- Environment: any (reproduced with a unit test, see below)
Client
- SDK: any SDK with data track support; triggered purely server-side
To Reproduce
- Subscriber calls
SubscribeToDataTrackfor a track that does not resolve
(never published, or already removed —subscribeDataTrackkeeps failing
withErrTrackNotFound, so no down track is created). - Subscriber calls
UnsubscribeFromDataTrackfor that track. reconcileWorkerpicks the track ID offreconcileDataTrackCh; with
desired == falseanddataDownTrack == nil, neitherneedsSubscribe()
norneedsUnsubscribe()is true, so it falls through to the cleanup branch
and deadlocks.
Unit test reproduction (times out on current master):
sm := newTestSubscriptionManager()
defer sm.Close(false)
// hasTrack=false -> subscribe attempts fail with ErrTrackNotFound
resolver := newTestDataTrackResolver(true, false, "pub", "pubID")
sm.params.DataTrackResolver = resolver.Resolve
sm.SubscribeToDataTrack("track")
time.Sleep(100 * time.Millisecond) // let the worker attempt (and fail) the subscribe
sm.UnsubscribeFromDataTrack("track")
// never succeeds: the worker is deadlocked, and sm.Close(false) hangs forever
require.Eventually(t, func() bool {
sm.lock.RLock()
_, ok := sm.dataTrackSubscriptions["track"]
sm.lock.RUnlock()
return !ok
}, 2*time.Second, 10*time.Millisecond)Goroutine dump at timeout shows the self-deadlock:
goroutine 47 [sync.Mutex.Lock]:
sync.(*RWMutex).Lock(...)
github.com/livekit/livekit-server/pkg/rtc.(*SubscriptionManager).notifyDataTrackSubscriberHandles(...)
pkg/rtc/subscriptionmanager.go:1160
github.com/livekit/livekit-server/pkg/rtc.(*SubscriptionManager).reconcileDataTrackSubscription(...)
pkg/rtc/subscriptionmanager.go:674 <- m.lock already held here
github.com/livekit/livekit-server/pkg/rtc.(*SubscriptionManager).reconcileWorker(...)
pkg/rtc/subscriptionmanager.go:726
goroutine 46 [sync.Mutex.Lock]:
github.com/livekit/livekit-server/pkg/rtc.(*SubscriptionManager).Close(...)
pkg/rtc/subscriptionmanager.go:105 <- blocked behind the worker
Expected behavior
Unsubscribing from an unresolved data track removes the subscription from
dataTrackSubscriptions; the reconcile worker keeps running and
SubscriptionManager.Close() returns.
Additional context
Fix is straightforward: call notifyDataTrackSubscriberHandles() after
releasing m.lock (the needsUnsubscribe branch directly above already does
exactly that), queue the not-found cleanup on the data track channel, and
include needsCleanup() in the periodic data track reconcile filter. PR
incoming.
Activity