Repository navigation
rtc: handle data track close after subscription installation - #5249
Conversation
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
|
@boks1971: PTAL |
| 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 { |
There was a problem hiding this comment.
This might have an issue if reconcile loop is changed. Can this be replaced with a deterministic way to check for a close?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed; the media-track counterpart belongs in a separate issue. This change remains confined to data tracks.
…unts Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
|
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})) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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()) { |
There was a problem hiding this comment.
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()) {
There was a problem hiding this comment.
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.
Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Perfloop Agent <[email protected]>
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.OnCloseimmediately 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.goadds 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 withIsSubscribedTo. 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.