go
runtime_error
ai_generated
true
WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (sync.Pool)
ID: go/goroutine-sync-pool-race
80%Fix Rate
83%Confidence
0Evidence
2025-09-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.3 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
Using sync.Pool without proper understanding; objects in pool may be accessed concurrently without synchronization.
generic中文
未正确理解sync.Pool的使用;池中的对象可能在没有同步的情况下被并发访问。
Workarounds
-
85% success Ensure pooled objects are not shared between goroutines
pool := &sync.Pool{New: func() interface{} { return &MyStruct{} }} go func() { obj := pool.Get().(*MyStruct) // use obj exclusively pool.Put(obj) }() -
80% success Use mutex inside pooled object if sharing is needed
type MyStruct struct { mu sync.Mutex data int } obj := pool.Get().(*MyStruct) obj.mu.Lock() obj.data = 42 obj.mu.Unlock() pool.Put(obj)
Dead Ends
Common approaches that don't work:
-
Assuming sync.Pool provides synchronization
90% fail
sync.Pool does not synchronize access to pooled objects; only the pool itself is safe.
-
Using global variables instead of pool
80% fail
Global variables are shared; race condition persists.