telemetry: add visibility into stats worker reference underflow (#4856)

* telemetry: add visibility into stats worker reference underflow

Log the paths that can leave a stats worker with no references, so the
`-1` never-closed cases seen in production can be traced to their origin.

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

* telemetry: guard StatsWorker.MarshalLogObject against a nil receiver

The new worker-created log passes the existing worker, which is a typed
nil when there is none. Mirror ReferenceGuard.MarshalLogObject.

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-11 14:34:23 +05:30
committed by GitHub
co-authored by Claude Fable 5.1
parent adf44a2eb6
commit d2405a72b1
3 changed files with 46 additions and 1 deletions
+36 -1
View File
@@ -286,6 +286,16 @@ func (t *telemetryService) getOrCreateWorker(
t.workersMu.Lock()
defer t.workersMu.Unlock()
if roomID == "" {
logger.Warnw(
"telemetry stats worker keyed under an empty room id", nil,
"room", roomName,
"participant", participantIdentity,
"participantID", participantID,
"guard", guard,
)
}
roomWorkers := t.workers[roomID]
worker, ok := roomWorkers[participantID]
if ok && !worker.Closed(guard) {
@@ -297,6 +307,21 @@ func (t *telemetryService) getOrCreateWorker(
existingIsConnected = worker.IsConnected()
}
// a guard references at most once, so a nil or already activated guard leaves the
// new worker with no references and its owner's release drives it negative
if guard == nil || guard.activated {
logger.Infow(
"telemetry stats worker created without a reference",
"room", roomName,
"roomID", roomID,
"participant", participantIdentity,
"participantID", participantID,
"guard", guard,
"replacedClosed", ok,
"existing", worker,
)
}
worker = newStatsWorker(
ctx,
t,
@@ -366,7 +391,17 @@ func (t *telemetryService) reKeyRoom(prevRoomID livekit.RoomID, roomID livekit.R
// only one worker can be keyed at (room, participant) and the one already
// filed there wins, close the superseded one so that it drains and is
// reaped instead of lingering in the flush list unreachable
if worker.ForceClose(survivor) {
forceClosed := worker.ForceClose(survivor)
logger.Infow(
"telemetry force closing superseded stats worker",
"prevRoomID", prevRoomID,
"roomID", roomID,
"participantID", participantID,
"forceClosed", forceClosed,
"superseded", worker,
"survivor", survivor,
)
if forceClosed {
prometheus.SubParticipant()
}
continue