Skip to content

Commit 7bc2a0b

Browse files
committed
double checked locking
1 parent 143dfc7 commit 7bc2a0b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

statemachine.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,13 @@ func (sm *StateMachine) stateRepresentation(state State) *stateRepresentation {
309309
sr, ok := sm.stateConfig[state]
310310
sm.stateMutex.RUnlock()
311311
if !ok {
312-
sr = newstateRepresentation(state)
313312
sm.stateMutex.Lock()
314-
sm.stateConfig[state] = sr
315-
sm.stateMutex.Unlock()
313+
defer sm.stateMutex.Unlock()
314+
// Check again, since another goroutine may have added it while we were waiting for the lock.
315+
if sr, ok = sm.stateConfig[state]; !ok {
316+
sr = newstateRepresentation(state)
317+
sm.stateConfig[state] = sr
318+
}
316319
}
317320
return sr
318321
}

0 commit comments

Comments
 (0)