From 7c82d3e103142508b6fd6eb1254858c904143515 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Fri, 11 Sep 2026 21:23:13 +0530 Subject: [PATCH] telemetry: do not recreate a stats worker for a released guard (#4860) * telemetry: do not recreate a stats worker for a released guard A ParticipantActive overtaken by the participant's close arrives with a guard ParticipantLeft already released and replaced the closed worker with one nothing could release. Co-Authored-By: Claude Fable 5.1 * telemetry: handle a released guard independently of map presence A released guard reaching getOrCreateWorker after the closed worker was reaped still created a zero-reference worker. Return nil as found instead, and make SetConnected nil-safe for ParticipantActive. Co-Authored-By: Claude Fable 5.1 --------- Co-authored-by: Claude Fable 5.1 --- pkg/telemetry/statsworker.go | 4 ++++ pkg/telemetry/statsworker_test.go | 33 +++++++++++++++++++++++++++++++ pkg/telemetry/telemetryservice.go | 8 ++++++++ 3 files changed, 45 insertions(+) diff --git a/pkg/telemetry/statsworker.go b/pkg/telemetry/statsworker.go index a6746dfb..03ec21a0 100644 --- a/pkg/telemetry/statsworker.go +++ b/pkg/telemetry/statsworker.go @@ -205,6 +205,10 @@ func (s *StatsWorker) sealStatsLocked() statsBatch { } func (s *StatsWorker) SetConnected() { + if s == nil { + return + } + s.lock.Lock() s.isConnected = true s.lock.Unlock() diff --git a/pkg/telemetry/statsworker_test.go b/pkg/telemetry/statsworker_test.go index 1963e44e..82997f46 100644 --- a/pkg/telemetry/statsworker_test.go +++ b/pkg/telemetry/statsworker_test.go @@ -1,10 +1,13 @@ package telemetry import ( + "context" "testing" "github.com/stretchr/testify/require" "go.uber.org/zap/zapcore" + + "github.com/livekit/protocol/livekit" ) func TestStatsWorker(t *testing.T) { @@ -67,3 +70,33 @@ func TestStatsWorker(t *testing.T) { require.NoError(t, w.MarshalLogObject(zapcore.NewMapObjectEncoder())) }) } + +func TestGetOrCreateWorkerReleasedGuard(t *testing.T) { + // ParticipantActive overtaken by the participant's close arrives with a guard that + // ParticipantLeft already released. It must not replace the closed worker with one + // nothing can release. + ts := &telemetryService{workers: make(map[livekit.RoomID]map[livekit.ParticipantID]*StatsWorker)} + roomID, pID := livekit.RoomID("room"), livekit.ParticipantID("participant") + + var g ReferenceGuard + w, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g) + require.False(t, found) + require.True(t, w.Close(&g)) + + t.Run("closed worker still in the map", func(t *testing.T) { + late, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g) + require.True(t, found) + require.Same(t, w, late) + require.Same(t, w, ts.workers[roomID][pID]) + }) + + t.Run("closed worker already reaped", func(t *testing.T) { + delete(ts.workers[roomID], pID) + + late, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g) + require.True(t, found) + require.Nil(t, late) + require.Empty(t, ts.workers[roomID]) + late.SetConnected() + }) +} diff --git a/pkg/telemetry/telemetryservice.go b/pkg/telemetry/telemetryservice.go index bc838a07..f316f6e4 100644 --- a/pkg/telemetry/telemetryservice.go +++ b/pkg/telemetry/telemetryservice.go @@ -302,6 +302,14 @@ func (t *telemetryService) getOrCreateWorker( return worker, true } + // only ParticipantLeft releases a guard, so a released guard is a call landing after + // the participant left, e.g. ParticipantActive overtaken by the close. Do not create + // a worker nothing can ever release. The closed worker, if not yet reaped, is returned + // as found, otherwise nil is + if guard != nil && guard.released { + return worker, true + } + existingIsConnected := false if ok { existingIsConnected = worker.IsConnected()