rtc: split EndSession out of MoveToRoom (#4917)

* rtc: split EndSession out of MoveToRoom

A participant's session in a room can end without the participant
closing: today on a move, and next on a room re-key, where a room ended
by the sweeper while participants were joining restarts under a new room
id with the same participants in it.

EndSession is the room-independent half of MoveToRoom: report the
published tracks unpublished, report the session end, run the caller's
leave under the ending session's telemetry guard, then take a fresh
guard and reset the deferred resolvers for the next session. The guard
matters: ParticipantLeft releases it and the telemetry service refuses
to open a stats worker for a released guard, so without a fresh one the
next session's join would silently get no worker.

MoveToRoom keeps the subscriber teardown and the onClose fan-out, passed
in as the leave.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* rtc: rename EndSession to LeaveSession, add JoinSession

EndSession read as terminal, but the participant survives it and joins
another session, so name the pair for what happens to the participant:
LeaveSession and JoinSession.

JoinSession is the other half that review pointed out was missing from
the API: the caller reports the join, then the published tracks are
reported published again under the new session, pairing the unpublish
LeaveSession reported. Without it the re-publish lived only in the
caller, with nothing in OSS proving the two halves balance.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Raja Subramanian
2026-09-28 13:41:30 +05:30
committed by GitHub
co-authored by Claude Fable 5.1
parent 93f5b3eb61
commit 6cfd19bd9b
4 changed files with 183 additions and 26 deletions
+47
View File
@@ -32,6 +32,7 @@ import (
"github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/logger/zaputil"
"github.com/livekit/protocol/observability/roomobs"
lksdp "github.com/livekit/protocol/sdp"
"github.com/livekit/protocol/signalling"
@@ -1052,3 +1053,49 @@ func TestResumedParticipantWaitsForReconnectResponse(t *testing.T) {
require.Equal(t, 1, sink.WriteMessageCallCount())
})
}
func TestLeaveJoinSession(t *testing.T) {
p := newParticipantForTest("leave-join-session")
p.params.LoggerResolver = zaputil.NoOpDeferrer{}
_, p.params.ReporterResolver = roomobs.DeferredParticipantReporter(roomobs.NewNoopProjectReporter())
tl := p.GetTelemetryListener().(*typesfakes.FakeParticipantTelemetryListener)
track := &typesfakes.FakeLocalMediaTrack{}
track.IDReturns("TR_test")
track.PublishedReturns(true)
p.UpTrackManager.AddPublishedTrack(track)
// leaving reports the track unpublished, then runs the leave under the left session's
// guard, and the next session gets a fresh one
prevGuard := p.TelemetryGuard()
require.NotNil(t, prevGuard)
left := false
p.LeaveSession(func() {
left = true
require.Same(t, prevGuard, p.TelemetryGuard())
require.Equal(t, 1, tl.OnTrackUnpublishedCallCount())
})
require.True(t, left)
require.NotSame(t, prevGuard, p.TelemetryGuard())
require.Equal(t, 1, tl.OnTrackUnpublishedCallCount())
_, _, _, wasPublished, shouldSend := tl.OnTrackUnpublishedArgsForCall(0)
require.True(t, wasPublished)
require.True(t, shouldSend)
// joining runs the join first, then reports the track published again
joined := false
p.JoinSession(func() {
joined = true
require.Zero(t, tl.OnTrackPublishedCallCount())
})
require.True(t, joined)
require.Equal(t, 1, tl.OnTrackPublishedCallCount())
_, _, _, shouldSend = tl.OnTrackPublishedArgsForCall(0)
require.True(t, shouldSend)
// nil callbacks are fine
p.LeaveSession(nil)
p.JoinSession(nil)
require.Equal(t, 2, tl.OnTrackUnpublishedCallCount())
require.Equal(t, 2, tl.OnTrackPublishedCallCount())
}