From a0a28ac5e3b3444d6eac4883535d55882601bd42 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Fri, 12 Dec 2025 12:42:04 +0530 Subject: [PATCH] Avoid duplicate track add to room track manager. (#4152) * Avoid duplicate track add to room track manager. Don't have proof that this happens, but in the leak chase, this is another component at room level and holds references to tracks. Guessing this is not cleaning tracks till room is closed. * add a report --- pkg/rtc/room.go | 2 +- pkg/rtc/roomtrackmanager.go | 45 ++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index d1bd5b1b..0bc158db 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -265,7 +265,6 @@ func NewRoom( agentClient: agentClient, agentStore: agentStore, agentDispatches: make(map[string]*agentDispatch), - trackManager: NewRoomTrackManager(), serverInfo: serverInfo, participants: make(map[livekit.ParticipantIdentity]types.LocalParticipant), participantOpts: make(map[livekit.ParticipantIdentity]*ParticipantOptions), @@ -284,6 +283,7 @@ func NewRoom( MaxSize: dataMessageCacheSize, }), } + r.trackManager = NewRoomTrackManager(r.logger) r.localParticipantListener = &localParticipantListener{room: r} if r.protoRoom.EmptyTimeout == 0 { diff --git a/pkg/rtc/roomtrackmanager.go b/pkg/rtc/roomtrackmanager.go index 6d4395de..7bb86224 100644 --- a/pkg/rtc/roomtrackmanager.go +++ b/pkg/rtc/roomtrackmanager.go @@ -22,11 +22,14 @@ import ( "github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/protocol/livekit" + "github.com/livekit/protocol/logger" "golang.org/x/exp/slices" ) // RoomTrackManager holds tracks that are published to the room type RoomTrackManager struct { + logger logger.Logger + lock sync.RWMutex changedNotifier *utils.ChangeNotifierManager removedNotifier *utils.ChangeNotifierManager @@ -46,8 +49,9 @@ type DataTrackInfo struct { PublisherID livekit.ParticipantID } -func NewRoomTrackManager() *RoomTrackManager { +func NewRoomTrackManager(logger logger.Logger) *RoomTrackManager { return &RoomTrackManager{ + logger: logger, tracks: make(map[livekit.TrackID][]*TrackInfo), dataTracks: make(map[livekit.TrackID][]*DataTrackInfo), changedNotifier: utils.NewChangeNotifierManager(), @@ -58,6 +62,16 @@ func NewRoomTrackManager() *RoomTrackManager { func (r *RoomTrackManager) AddTrack(track types.MediaTrack, publisherIdentity livekit.ParticipantIdentity, publisherID livekit.ParticipantID) { trackID := track.ID() r.lock.Lock() + infos, ok := r.tracks[trackID] + if ok { + for _, info := range infos { + if info.Track == track { + r.lock.Unlock() + r.logger.Infow("not adding duplicate track", "trackID", trackID) + return + } + } + } r.tracks[trackID] = append(r.tracks[trackID], &TrackInfo{ Track: track, PublisherIdentity: publisherIdentity, @@ -79,6 +93,7 @@ func (r *RoomTrackManager) RemoveTrack(track types.MediaTrack) { } found := false + numRemoved := 0 for idx, info := range infos { if info.Track == track { r.tracks[trackID] = slices.Delete(r.tracks[trackID], idx, idx+1) @@ -86,10 +101,13 @@ func (r *RoomTrackManager) RemoveTrack(track types.MediaTrack) { delete(r.tracks, trackID) } found = true - break + numRemoved++ } } r.lock.Unlock() + if numRemoved > 1 { + r.logger.Warnw("removed multiple tracks", nil, "trackID", trackID, "numRemoved", numRemoved) + } if !found { return @@ -157,6 +175,16 @@ func (r *RoomTrackManager) GetOrCreateTrackRemoveNotifier(trackID livekit.TrackI func (r *RoomTrackManager) AddDataTrack(dataTrack types.DataTrack, publisherIdentity livekit.ParticipantIdentity, publisherID livekit.ParticipantID) { trackID := dataTrack.ID() r.lock.Lock() + infos, ok := r.dataTracks[trackID] + if ok { + for _, info := range infos { + if info.DataTrack == dataTrack { + r.lock.Unlock() + r.logger.Infow("not adding duplicate data track", "trackID", trackID) + return + } + } + } r.dataTracks[trackID] = append(r.dataTracks[trackID], &DataTrackInfo{ DataTrack: dataTrack, PublisherIdentity: publisherIdentity, @@ -178,6 +206,7 @@ func (r *RoomTrackManager) RemoveDataTrack(dataTrack types.DataTrack) { } found := false + numRemoved := 0 for idx, info := range infos { if info.DataTrack == dataTrack { r.dataTracks[trackID] = slices.Delete(r.dataTracks[trackID], idx, idx+1) @@ -185,10 +214,13 @@ func (r *RoomTrackManager) RemoveDataTrack(dataTrack types.DataTrack) { delete(r.dataTracks, trackID) } found = true - break + numRemoved++ } } r.lock.Unlock() + if numRemoved > 1 { + r.logger.Warnw("removed multiple data tracks", nil, "trackID", trackID, "numRemoved", numRemoved) + } if !found { return @@ -215,3 +247,10 @@ func (r *RoomTrackManager) GetDataTrackInfo(trackID livekit.TrackID) *DataTrackI // earliest added data track is used till it is removed return infos[0] } + +func (r *RoomTrackManager) Report() { + r.lock.RLock() + defer r.lock.RUnlock() + + r.logger.Infow("room track manager report", "numTracks", len(r.tracks), "numDataTracks", len(r.dataTracks)) +}