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
This commit is contained in:
Raja Subramanian
2025-12-12 12:42:04 +05:30
committed by GitHub
parent f01008f876
commit a0a28ac5e3
2 changed files with 43 additions and 4 deletions
+1 -1
View File
@@ -265,7 +265,6 @@ func NewRoom(
agentClient: agentClient, agentClient: agentClient,
agentStore: agentStore, agentStore: agentStore,
agentDispatches: make(map[string]*agentDispatch), agentDispatches: make(map[string]*agentDispatch),
trackManager: NewRoomTrackManager(),
serverInfo: serverInfo, serverInfo: serverInfo,
participants: make(map[livekit.ParticipantIdentity]types.LocalParticipant), participants: make(map[livekit.ParticipantIdentity]types.LocalParticipant),
participantOpts: make(map[livekit.ParticipantIdentity]*ParticipantOptions), participantOpts: make(map[livekit.ParticipantIdentity]*ParticipantOptions),
@@ -284,6 +283,7 @@ func NewRoom(
MaxSize: dataMessageCacheSize, MaxSize: dataMessageCacheSize,
}), }),
} }
r.trackManager = NewRoomTrackManager(r.logger)
r.localParticipantListener = &localParticipantListener{room: r} r.localParticipantListener = &localParticipantListener{room: r}
if r.protoRoom.EmptyTimeout == 0 { if r.protoRoom.EmptyTimeout == 0 {
+42 -3
View File
@@ -22,11 +22,14 @@ import (
"github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
// RoomTrackManager holds tracks that are published to the room // RoomTrackManager holds tracks that are published to the room
type RoomTrackManager struct { type RoomTrackManager struct {
logger logger.Logger
lock sync.RWMutex lock sync.RWMutex
changedNotifier *utils.ChangeNotifierManager changedNotifier *utils.ChangeNotifierManager
removedNotifier *utils.ChangeNotifierManager removedNotifier *utils.ChangeNotifierManager
@@ -46,8 +49,9 @@ type DataTrackInfo struct {
PublisherID livekit.ParticipantID PublisherID livekit.ParticipantID
} }
func NewRoomTrackManager() *RoomTrackManager { func NewRoomTrackManager(logger logger.Logger) *RoomTrackManager {
return &RoomTrackManager{ return &RoomTrackManager{
logger: logger,
tracks: make(map[livekit.TrackID][]*TrackInfo), tracks: make(map[livekit.TrackID][]*TrackInfo),
dataTracks: make(map[livekit.TrackID][]*DataTrackInfo), dataTracks: make(map[livekit.TrackID][]*DataTrackInfo),
changedNotifier: utils.NewChangeNotifierManager(), changedNotifier: utils.NewChangeNotifierManager(),
@@ -58,6 +62,16 @@ func NewRoomTrackManager() *RoomTrackManager {
func (r *RoomTrackManager) AddTrack(track types.MediaTrack, publisherIdentity livekit.ParticipantIdentity, publisherID livekit.ParticipantID) { func (r *RoomTrackManager) AddTrack(track types.MediaTrack, publisherIdentity livekit.ParticipantIdentity, publisherID livekit.ParticipantID) {
trackID := track.ID() trackID := track.ID()
r.lock.Lock() 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{ r.tracks[trackID] = append(r.tracks[trackID], &TrackInfo{
Track: track, Track: track,
PublisherIdentity: publisherIdentity, PublisherIdentity: publisherIdentity,
@@ -79,6 +93,7 @@ func (r *RoomTrackManager) RemoveTrack(track types.MediaTrack) {
} }
found := false found := false
numRemoved := 0
for idx, info := range infos { for idx, info := range infos {
if info.Track == track { if info.Track == track {
r.tracks[trackID] = slices.Delete(r.tracks[trackID], idx, idx+1) 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) delete(r.tracks, trackID)
} }
found = true found = true
break numRemoved++
} }
} }
r.lock.Unlock() r.lock.Unlock()
if numRemoved > 1 {
r.logger.Warnw("removed multiple tracks", nil, "trackID", trackID, "numRemoved", numRemoved)
}
if !found { if !found {
return 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) { func (r *RoomTrackManager) AddDataTrack(dataTrack types.DataTrack, publisherIdentity livekit.ParticipantIdentity, publisherID livekit.ParticipantID) {
trackID := dataTrack.ID() trackID := dataTrack.ID()
r.lock.Lock() 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{ r.dataTracks[trackID] = append(r.dataTracks[trackID], &DataTrackInfo{
DataTrack: dataTrack, DataTrack: dataTrack,
PublisherIdentity: publisherIdentity, PublisherIdentity: publisherIdentity,
@@ -178,6 +206,7 @@ func (r *RoomTrackManager) RemoveDataTrack(dataTrack types.DataTrack) {
} }
found := false found := false
numRemoved := 0
for idx, info := range infos { for idx, info := range infos {
if info.DataTrack == dataTrack { if info.DataTrack == dataTrack {
r.dataTracks[trackID] = slices.Delete(r.dataTracks[trackID], idx, idx+1) 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) delete(r.dataTracks, trackID)
} }
found = true found = true
break numRemoved++
} }
} }
r.lock.Unlock() r.lock.Unlock()
if numRemoved > 1 {
r.logger.Warnw("removed multiple data tracks", nil, "trackID", trackID, "numRemoved", numRemoved)
}
if !found { if !found {
return return
@@ -215,3 +247,10 @@ func (r *RoomTrackManager) GetDataTrackInfo(trackID livekit.TrackID) *DataTrackI
// earliest added data track is used till it is removed // earliest added data track is used till it is removed
return infos[0] 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))
}