From f6608977f08a779584f9246553204912951167b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Fuente=20P=C3=A9rez?= Date: Tue, 23 Jan 2024 02:11:34 +0100 Subject: [PATCH] Fix race condition on Participant.updateState (#2401) The comparisson between the last and current ParticipantInfo_State wasn't atomic. This sometimes resulted in two calls to onStateChange method for the same participant state. In the end this was reflected in two ACTIVE events being generated for the same participant at exactly the same moment. The fix actually uses the atomic method Swap to properly protect the "compare and set" operation and avoid any race condition. --- pkg/rtc/participant.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index bfb3112c..de62da35 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -1242,12 +1242,11 @@ func (p *ParticipantImpl) setupParticipantTrafficLoad() { func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) { oldState := p.State() - if state == oldState { + if !(p.state.Swap(state) != state) { return } p.params.Logger.Debugw("updating participant state", "state", state.String()) - p.state.Store(state) p.dirty.Store(true) p.lock.RLock()