Skip to content

rtc: handle data track close after subscription installation - #5249

Merged
boks1971 merged 9 commits into
livekit:masterfrom
perfloop-oss:perfloop-pr-open-ef69ywhv49
Nov 23, 2026
Merged

boks1971 merged 9 commits into
livekit:masterfrom
perfloop-oss:perfloop-pr-open-ef69ywhv49

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Revoking a data track during automatic subscription can make it impossible to resubscribe after permission returns, or leave a participant listed as subscribed when it is not.

Defer the downtrack’s close-callback registration until the end of installation, rather than registering before storage. DataDownTrack.OnClose immediately delivers an earlier close, so cleanup now sees both the stored downtrack and any publisher mark. Registration also runs when a permission recheck rejects the subscription. The existing permission rechecks stay in place, with no new locks or public APIs.

pkg/rtc/room_datatrack_test.go adds a regression for each ordering through room publish/join auto-subscription, real permission revocation, and real data tracks and downtracks. Both check recovery after regrant; the later-close case also checks subscribed-participant lists through denied retries. The later-close test guards the pre-mark window with IsSubscribedTo. It waits for the denied update while requiring real subscriber removal, an empty manager downtrack list and no publisher membership in either query.

The race window is a few instructions inside subscribeDataTrack; the tests inject the close there, and explicit subscriptions use the same function.

Focused Go race tests for data-track installation, options, permission changes and resubscription passed; both close-ordering regressions failed without the repair and passed with it. Both regrant checks restored the real subscriber, manager downtrack and publisher membership; the later-close check also left neither publisher-membership query retaining the revoked subscription.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@perfloop-agent
perfloop-agent requested a review from a team as a code owner November 20, 2026 14:32

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

@boks1971: PTAL

Comment thread pkg/rtc/room_datatrack_test.go Outdated
var calls atomic.Int32
sub.params.ParticipantHelper.(*typesfakes.FakeLocalParticipantHelper).ResolveDataTrackCalls(func(p types.LocalParticipant, id livekit.TrackID) types.DataResolverResult {
res := room.ResolveDataTrackForSubscriber(p, id)
if calls.Add(1) == 2 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might have an issue if reconcile loop is changed. Can this be replaced with a deterministic way to check for a close?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes—the test now revokes only after the real track records the subscriber and waits for the denied permission update before checking cleanup, instead of counting resolver calls. This ties the check to subscription state rather than the number of reconciliation steps.

// Install the close callback after both the downtrack and publisher mark.
// OnClose also delivers an earlier close synchronously, so it must run
// after installation even when a permission recheck returns an error.
defer dataDownTrack.OnClose(func() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this.

This also exists in the media tracks path. The difference there is that the SubscribedTrack does not replay the close. I think that also should get the same behaviour as data down track.

We can do a separate issue for that. Just wanted to mention here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; the media-track counterpart belongs in a separate issue. This change remains confined to data tracks.

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The production fix is unchanged; this update makes the lifecycle test independent of resolver call counts.

require.Empty(t, sub.GetSubscribedDataTracks())
assert.False(t, sub.IsSubscribedTo(pub.ID()), "closed subscription must not mark the publisher")
assert.NotContains(t, sub.GetSubscribedParticipants(), pub.ID(), "closed subscription must not list the publisher")
require.NoError(t, pub.HandleUpdateSubscriptionPermission(&livekit.SubscriptionPermission{AllParticipants: true}))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting a couple of more gaps from an agent review

Optional: the checks after the denied update rely on that update being sent only from a later subscribe attempt. To keep the test correct if that changes, the wait could cover the end state directly:

    require.EventuallyWithT(t, func(c *assert.CollectT) {
        assert.True(c, deniedUpdateSent(), "closed subscription must retry and report denied permission")
        assert.False(c, track.IsSubscriber(sub.ID()))
        assert.Empty(c, sub.GetSubscribedDataTracks())
        assert.False(c, sub.IsSubscribedTo(pub.ID()), "closed subscription must not mark the publisher")
        assert.NotContains(c, sub.GetSubscribedParticipants(), pub.ID(), "closed subscription must not list the publisher")
    }, time.Second, time.Millisecond)

where `deniedUpdateSent` is the existing loop over the sink messages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test now waits for the denied update and all four cleanup conditions together using the suggested EventuallyWithT block. It no longer treats the denied update alone as completion.

Comment thread pkg/rtc/room_datatrack_test.go Outdated
var once sync.Once
sub.params.ParticipantHelper.(*typesfakes.FakeLocalParticipantHelper).ResolveDataTrackCalls(func(p types.LocalParticipant, id livekit.TrackID) types.DataResolverResult {
res := room.ResolveDataTrackForSubscriber(p, id)
if res.DataTrack != nil && res.HasPermission && res.DataTrack.IsSubscriber(sub.ID()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From an agent review

Optional: the revoke trigger fires on the first resolver call that sees the subscriber recorded with permission. Today that is the recheck in `subscribeDataTrack`, so the revoke lands before `markSubscribedTo`. If a resolver call is ever added after the mark, the revoke would land after it and this test would pass without the fix. Adding a guard keeps the trigger inside the window no matter who calls the resolver:

    if res.DataTrack != nil && res.HasPermission && res.DataTrack.IsSubscriber(sub.ID()) && !sub.IsSubscribedTo(pub.ID()) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the suggested pre-mark guard, so the revoke only fires while the real subscriber exists and publisher membership is still absent. The production fix is unchanged.

@boks1971 boks1971 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 🙏

@boks1971
boks1971 merged commit 788c418 into livekit:master Nov 23, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants