A close during data-track installation blocks resubscription after regrant
perfloop-oss/livekit · Missed notification
https://perfloop.ai/t/oss/case_6ffxq13p5m
Verdict
VERIFIED · settled 2026-11-23 · merged as livekit/livekit#5249
What happened: The check fails before this change and passes with it.
Hypothesis
A close during data-track installation can leave the manager holding a closed downtrack. It then treats the requested subscription as fulfilled, even after permission returns and reconciliation runs. A native replay demonstrates this ordering; its production frequency is unknown.
SubscriptionManager.subscribeDataTrack registers OnClose at lines 1189–1191 before storing the downtrack at line 1192. The real DataDownTrack.OnClose immediately invokes a newly registered callback when already closed (pkg/rtc/datadowntrack.go:87–95). handleDataDownTrackClose returns when the stored pointer is nil (subscriptionmanager.go:1233–1239). Thus a publisher revoke after DataTrack.AddSubscriber records its subscriber but before manager storage delivers a close that clears nothing. The manager subsequently stores the closed downtrack. The post-install permission check removes an already removed subscriber and returns an error, but needsSubscribe requires a nil downtrack (1722–1725), so later reconciliations cannot retry.
The new package-native pkg/rtc/perfloop_data_close_check_test.go completely enumerated seven named schedules: no revoke, revoke before the request, before the real add, after the real add but before callback registration, after registration but before storage, after storage before the recheck, and after successful installation. Bounds were one participant, one real DataTrack, real DataDownTracks, one revoke/regrant, and three post-grant reconciliations. Wrappers scheduled events only between existing unlocked calls; the existing reconciliation seam was driven synchronously. Command: GOMAXPROCS=2 go test -p=2 ./pkg/rtc -run '^TestPerfloopDataCloseSearch$' -count=1 -timeout=90s. It exited 1 with 'complete named-schedule search: 2/7 violations'. Both failing schedules closed before storage. The other five schedules were controls, not an unbounded assurance result.
The reduced TestPerfloopDataCloseReplay runs only the after-add-before-registration ordering using the real RevokeDisallowedSubscribers, RemoveSubscriber, Close, and OnClose implementations. Command: GOMAXPROCS=2 go test -p=2 ./pkg/rtc -run '^TestPerfloopDataCloseReplay$' -count=1 -timeout=90s. It exited 1 with 'after regrant: real_subscriber=false manager_has_downtrack=true needs_subscribe=false add_calls=1', even with an explicit repeated SubscribeToDataTrack request. The denied-state check also retained has_permission=true. Only a new test file was used, and it was removed afterward.
The Case proof target is a coherent installation/close handoff across early and ordinary close delivery, including recovery when the same desired subscription becomes permitted again. The first useful check is this deterministic replay against a provisional handoff repair, retaining the close-after-storage and non-revoked controls. The checker layout is provisional, not a required implementation. Coordination must account for synchronous callbacks without introducing a lock cycle; callback-order compatibility and any added synchronization remain to assess. This is a concrete supported lifecycle failure, not speculative security hardening.
Merged https://github.com/livekit/livekit/pull/5196 (529bd357b175770e9c199b2e766afa39cc3e9279) adds the permission recheck already present in this checkout. Its test delivers a fake downtrack's close manually after registration, so that change does not cover this earlier close/storage ordering. The media counterpart https://github.com/livekit/livekit/pull/5177 likewise addresses permission rechecking, not this handoff. Open https://github.com/livekit/livekit/pull/4656 changes data-track demand reporting and leaves this manager ordering unchanged. All open-PR pages were read. A bounded public Developer Community search for '"data track" subscription' returned no posts; private Slack discussion is unavailable. The checkout is shallow, and the bounded file-history API read returned HTTP 403, leaving recent merged-history coverage incomplete beyond the inspected implementation records. Prior permission finding hyp_5jp1a02qsg concerns overlapping authorization-update commits, not close delivery during subscriber installation.
Change to test: Coordinate downtrack installation with terminal close delivery so a close cannot be acknowledged before there is subscription state to clear. Keep a closed installation retryable and preserve permission updates and subscriber-handle notifications through a later regrant.
Where it lives
perfloop-oss/livekit · pkg/rtc/subscriptionmanager.go
Evidence
The check fails before this change and passes with it: `For a local room auto-subscription, a publisher revoke after the real data-track add but before close-callback registration must not prevent a subsequent regrant from restoring the real subscriber, manager downtrack, and publisher membership.`
The check fails before this change and passes with it: `For a local room data-track auto-subscription, a publisher revoke after the final permitted resolver snapshot and before publisher marking must leave no real subscriber or manager downtrack, and neither publisher-membership query may retain the publisher through subsequent denied retries; a subsequent regrant restores the subscription.`
Data-track subscribe and unsubscribe · 30 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
12345 |
12364 |
+13.5 |
−366 to +581 |
no regression (noise ≤ min(0.05, 0.05) × |baseline median|) |
PASSED |
Checks: 3 of 3 passed. Verification: no defect found.