clientPointer, myClientPointer and killChannel are created once at startup and
passed by reference into eleven packages. All of them read and wrote those plain
Go maps from different goroutines, with no synchronisation.
Go does not tolerate that: the runtime kills the process with
fatal error: concurrent map writes
and a fatal error is not recoverable — a deferred recover() does not catch it.
Measured in production: the process died inside StartClient while five instances
were connecting at once. The orchestrator restarted it within seconds, but every
number was offline in the meantime, and it repeats on any restart that brings up
more than a couple of instances together.
The three maps become safemap.Map[T], a generic wrapper over a RWMutex. It lives
in its own package because MyClient is defined in pkg/whatsmeow/service, and
putting the type there would create an import cycle with the ten packages that
receive the maps.
98 call sites changed across 12 files, with the compiler as the judge: anything
missed is a build error, not a silent bug.
Tests: with -race, a plain map running the same exercise reports DATA RACE and
fails, while safemap.Map passes. Both halves verified.
(cherry picked from commit 3860fa278dc2b901705ffbddbed5cf59d70a85e4)
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package safemap
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// TestPlainMapRaces is the calibration test: it proves the detector can see the
|
|
// bug in the first place.
|
|
//
|
|
// It drives a plain Go map the way this code used to — N goroutines writing the
|
|
// same key. Under -race the detector flags it. Without -race the runtime kills
|
|
// the process with "fatal error: concurrent map writes", which is exactly the
|
|
// failure this package exists to prevent.
|
|
//
|
|
// Skipped by default precisely because that failure is not recoverable and
|
|
// would take the whole suite down. Run it on purpose to confirm the detector is
|
|
// not blind:
|
|
//
|
|
// go test -race -run TestPlainMapRaces ./pkg/safemap/
|
|
func TestPlainMapRaces(t *testing.T) {
|
|
t.Skip("calibration: run by hand — see the comment above")
|
|
|
|
m := map[string]int{}
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 50; i++ {
|
|
wg.Add(1)
|
|
go func(n int) {
|
|
defer wg.Done()
|
|
m["key"] = n // <- the race
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
// TestConcurrentAccess runs the same exercise through the guarded map: fifty
|
|
// goroutines writing, reading and ranging over the same key at once — the shape
|
|
// of a startup where several instances connect simultaneously.
|
|
func TestConcurrentAccess(t *testing.T) {
|
|
m := New[int]()
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 50; i++ {
|
|
wg.Add(3)
|
|
go func(n int) { defer wg.Done(); m.Set("key", n) }(i)
|
|
go func() { defer wg.Done(); _ = m.Get("key") }()
|
|
go func() {
|
|
defer wg.Done()
|
|
_, _ = m.Lookup("key")
|
|
_ = m.Len()
|
|
for range m.Snapshot() {
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
|
|
if _, ok := m.Lookup("key"); !ok {
|
|
t.Fatal("key should still be in the map")
|
|
}
|
|
}
|
|
|
|
// TestGetMissingKeyReturnsZero pins the behaviour the call sites relied on:
|
|
// m[k] on an absent key yields the zero value, and Get must do the same —
|
|
// otherwise swapping the call sites would have changed logic silently.
|
|
func TestGetMissingKeyReturnsZero(t *testing.T) {
|
|
if v := New[*int]().Get("absent"); v != nil {
|
|
t.Fatalf("want nil, got %v", v)
|
|
}
|
|
if n := New[int]().Get("absent"); n != 0 {
|
|
t.Fatalf("want 0, got %d", n)
|
|
}
|
|
}
|