Prevent operating on swapped out map. (#3670)

* Prevent operating on swapped out map.

* test
This commit is contained in:
Raja Subramanian
2025-05-17 17:02:41 +05:30
committed by GitHub
parent 13b55a80ad
commit 5f87a35b7e
2 changed files with 8 additions and 10 deletions
+6 -9
View File
@@ -1338,14 +1338,15 @@ func (r *Room) broadcastParticipantState(p types.LocalParticipant, opts broadcas
return return
} }
r.batchedUpdatesMu.Lock()
updates := PushAndDequeueUpdates( updates := PushAndDequeueUpdates(
pi, pi,
p.CloseReason(), p.CloseReason(),
opts.immediate, opts.immediate,
r.GetParticipant(livekit.ParticipantIdentity(pi.Identity)), r.GetParticipant(livekit.ParticipantIdentity(pi.Identity)),
&r.batchedUpdatesMu,
r.batchedUpdates, r.batchedUpdates,
) )
r.batchedUpdatesMu.Unlock()
if len(updates) != 0 { if len(updates) != 0 {
selfSent = true selfSent = true
SendParticipantUpdates(updates, r.GetParticipants()) SendParticipantUpdates(updates, r.GetParticipants())
@@ -1395,14 +1396,14 @@ func (r *Room) changeUpdateWorker() {
r.sendRoomUpdate() r.sendRoomUpdate()
case <-subTicker.C: case <-subTicker.C:
r.batchedUpdatesMu.Lock() r.batchedUpdatesMu.Lock()
if len(r.batchedUpdates) == 0 {
r.batchedUpdatesMu.Unlock()
continue
}
updatesMap := r.batchedUpdates updatesMap := r.batchedUpdates
r.batchedUpdates = make(map[livekit.ParticipantIdentity]*ParticipantUpdate) r.batchedUpdates = make(map[livekit.ParticipantIdentity]*ParticipantUpdate)
r.batchedUpdatesMu.Unlock() r.batchedUpdatesMu.Unlock()
if len(updatesMap) == 0 {
continue
}
SendParticipantUpdates(maps.Values(updatesMap), r.GetParticipants()) SendParticipantUpdates(maps.Values(updatesMap), r.GetParticipants())
} }
} }
@@ -1819,12 +1820,8 @@ func PushAndDequeueUpdates(
closeReason types.ParticipantCloseReason, closeReason types.ParticipantCloseReason,
isImmediate bool, isImmediate bool,
existingParticipant types.Participant, existingParticipant types.Participant,
lock *sync.Mutex,
cache map[livekit.ParticipantIdentity]*ParticipantUpdate, cache map[livekit.ParticipantIdentity]*ParticipantUpdate,
) []*ParticipantUpdate { ) []*ParticipantUpdate {
lock.Lock()
defer lock.Unlock()
var updates []*ParticipantUpdate var updates []*ParticipantUpdate
identity := livekit.ParticipantIdentity(pi.Identity) identity := livekit.ParticipantIdentity(pi.Identity)
existing := cache[identity] existing := cache[identity]
+2 -1
View File
@@ -351,14 +351,15 @@ func TestPushAndDequeueUpdates(t *testing.T) {
if tc.existing != nil { if tc.existing != nil {
rm.batchedUpdates[livekit.ParticipantIdentity(tc.existing.ParticipantInfo.Identity)] = tc.existing rm.batchedUpdates[livekit.ParticipantIdentity(tc.existing.ParticipantInfo.Identity)] = tc.existing
} }
rm.batchedUpdatesMu.Lock()
updates := PushAndDequeueUpdates( updates := PushAndDequeueUpdates(
tc.pi, tc.pi,
tc.closeReason, tc.closeReason,
tc.immediate, tc.immediate,
rm.GetParticipant(livekit.ParticipantIdentity(tc.pi.Identity)), rm.GetParticipant(livekit.ParticipantIdentity(tc.pi.Identity)),
&rm.batchedUpdatesMu,
rm.batchedUpdates, rm.batchedUpdates,
) )
rm.batchedUpdatesMu.Unlock()
require.Equal(t, len(tc.expected), len(updates)) require.Equal(t, len(tc.expected), len(updates))
for i, item := range tc.expected { for i, item := range tc.expected {
requirePIEquals(t, item.ParticipantInfo, updates[i].ParticipantInfo) requirePIEquals(t, item.ParticipantInfo, updates[i].ParticipantInfo)