Old observer cleanup deletes a replacement subscription's notifications

perfloop-oss/livekit · Identity Collision

https://perfloop.ai/t/oss/case_146ydt8tdv

Verdict

VERIFIED · settled 2026-11-23

What happened: The check fails before this change and passes with it.

Hypothesis

Cleanup from an old subscription can delete the observers belonging to its replacement. That loses subsequent track-change and source-removal notifications. A deterministic native replay demonstrates the collision during an ordinary unsubscribe/resubscribe sequence; the frequency of delayed cleanup in production is unknown.

subscribeDataTrack registers both observers using only string(sub.subscriberID), at subscriptionmanager.go:1150 and 1155. The shared trackSubscription helpers detach them with asynchronous RemoveObserver calls using that same key (1478–1488 and 1498–1508). A replacement subscription can register before the old cleanup goroutine executes. ChangeNotifier.AddObserver replaces the key's callback, while RemoveObserver unconditionally deletes the key (pkg/utils/changenotifier.go:32–43), so delayed old cleanup deletes the new registration. Room.ResolveDataTrackForSubscriber obtains room-managed GetOrCreate notifiers for the track (pkg/rtc/room.go:732–753), allowing the same notifier to be reused across subscription generations.

The new package-native pkg/rtc/perfloop_data_notifier_check_test.go completely enumerated four bounded histories: changed and removed notifiers, each with old detach completing before or after replacement attach. Each history used one participant, one real DataTrack, real DataDownTracks, the real ChangeNotifier map implementation, and two subscription generations. A wrapper blocked only the first RemoveObserver before its real map mutation; channels selected the order and two-second waits were watchdogs. Public SubscribeToDataTrack and UnsubscribeFromDataTrack requests were processed through the existing package reconciliation seam. Command: GOMAXPROCS=2 go test -p=2 ./pkg/rtc -run '^TestPerfloopDataNotifierSearch$' -count=1 -timeout=90s. It exited 1 with 'complete detach/attach search: 2/4 violations'. Both delayed-detach histories lost the replacement observer; both serial controls retained notification delivery.

The reduced TestPerfloopDataNotifierReplay runs only the removed-notifier history: old unsubscribe begins cleanup, a new subscription attaches under the same key, old cleanup finishes, then the source becomes unavailable and sends its removal notification. Command: GOMAXPROCS=2 go test -p=2 ./pkg/rtc -run '^TestPerfloopDataNotifierReplay$' -count=1 -timeout=90s. It exited 1 with 'kind=removed detach_after_attach=true replacement_observer=false notification_effect=false queued=0 desired=true'. The source-removal callback is intended to clear desired (handleSourceDataTrackRemoved and trackSubscription.handleSourceTrackRemoved, 1270–1277 and 1512–1522). The changed-notifier witness instead lost the immediate reconcile wakeup; periodic retries remain a separate recovery route. Only a new test file was used, and it was removed afterward.

The Case target is ownership-safe observer retirement across subscription replacement, covering both notifier kinds rather than fixing only the removal witness. The shared helper also belongs to mediaTrackSubscription (1526–1528), so its sibling attachment paths must be assessed rather than excluded because this fixture uses data tracks. The smallest useful next check is the delayed-detach replay against a provisional registration-ownership repair, with detach-before-attach and notifier-replacement controls. The test layout is provisional. The supported trigger is a cleanup goroutine lagging a new registration; a repair may add per-registration identity or ordering state, whose complexity and lock-order effects remain to assess.

This is independent of hyp_cckxdtvr02 and hyp_rcy148wmkp: neither downtrack callback/storage ordering nor publisher marking prevents an old observer removal from deleting a new callback. Specific prior searches for RemoveObserver and the shared notifier owner returned no matching repair. Inspected https://github.com/livekit/livekit/pull/5196 adds permission rechecking, and open https://github.com/livekit/livekit/pull/4656 adds demand reporting; neither changes observer ownership. All open-PR pages and relevant overlapping hunks were read. The public Developer Community search returned no matching posts. Private Slack and additional merged file history remain named gaps: Slack is unavailable, and the bounded history read was refused with HTTP 403 on a shallow checkout.

Change to test: Retire only the observer registration owned by the departing subscription, without deleting a later registration for the same participant. Make both changed- and removed-notifier attachment and cleanup ownership-safe in the shared subscription lifecycle, while preserving its lock-order constraints.

Where it lives

perfloop-oss/livekit · pkg/rtc/subscriptionmanager.go

Evidence

The check fails before this change and passes with it: `For a joined room participant, a desired data-track subscription retains track-change wakeups and source-removal notifications after close while desired, unsubscribe followed by resubscribe, and back-to-back subscription toggles before reconciliation.`

The check fails before this change and passes with it: `For a joined room participant, a desired media-track subscription retains track-change wakeups and source-removal notifications after close while desired, unsubscribe followed by resubscribe, and back-to-back subscription toggles before reconciliation.`

The check fails before this change and passes with it: `After unsubscribe and a settings update, reconciliation removes changed and removed observers from media and data subscriptions with no installed track before their settings hold expires, preserves those settings for resubscription, and restores observers on the next subscribe.`

Checks: 5 of 5 passed. Verification: no defect found.

Timeline