Skip to content

Data track subscription cleanup deadlocks the subscription reconcile worker #4842

Closed

Description

@tcoreal

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 on doneCh, hanging
    participant teardown and leaking the associated resources.
    There are two adjacent bugs in the same flow that also prevent cleanup from
    working:
  1. In the ErrTrackNotFound branch, after notFoundTimeout the data track ID
    is queued on the media reconcile channel (m.queueReconcile(s.trackID)
    instead of m.queueReconcileDataTrack(s.trackID)), so the intended
    immediate cleanup never runs.
  2. The periodic reconcileDataTrackSubscriptions() only selects subscriptions
    with needsSubscribe() || needsUnsubscribe() — unlike its media counterpart
    it omits needsCleanup(), so an abandoned data track subscription
    (!desired && dataDownTrack == nil) is never reaped by the ticker either
    and leaks in dataTrackSubscriptions.
    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

  1. Subscriber calls SubscribeToDataTrack for a track that does not resolve
    (never published, or already removed — subscribeDataTrack keeps failing
    with ErrTrackNotFound, so no down track is created).
  2. Subscriber calls UnsubscribeFromDataTrack for that track.
  3. reconcileWorker picks the track ID off reconcileDataTrackCh; with
    desired == false and dataDownTrack == nil, neither needsSubscribe()
    nor needsUnsubscribe() 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions