fix(package): [concurrency] hold the map lock while taking the try lock (#417)

* fix(package): [concurrency] hold the map lock while taking the try lock

* test(package): [concurrency] check TryKeyedLocker mutual exclusion
This commit is contained in:
Lee Sang Gyu authored and GitHub committed 2026-11-09 17:36:58 +08:00
1 parent d859b91c57
commit b09d6af4a4
2 files changed
+36 -1

No files matched your search

+32
View File
@@ -4,6 +4,7 @@ import (
"context"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
@@ -228,3 +229,34 @@ func TestTryKeyedLocker_ParallelTry(t *testing.T) {
assert.Equal(5, count)
assert.Equal(0, len(locker.locks))
}
func TestTryKeyedLocker_MutualExclusionUnderContention(t *testing.T) {
t.Parallel()
locker := NewTryKeyedLocker[string]()
var holders, violations int32
var wg sync.WaitGroup
for g := 0; g < 8; g++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 20000; i++ {
if !locker.TryLock("key") {
continue
}
if atomic.AddInt32(&holders, 1) > 1 {
atomic.AddInt32(&violations, 1)
}
atomic.AddInt32(&holders, -1)
locker.Unlock("key")
}
}()
}
wg.Wait()
if v := atomic.LoadInt32(&violations); v != 0 {
t.Fatalf("the lock was held by more than one goroutine %d times", v)
}
}