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.
This commit is contained in:
Pablo Fuente Pérez
2024-01-22 17:11:34 -08:00
committed by GitHub
parent 867325d120
commit f6608977f0
+1 -2
View File
@@ -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()