go
runtime_error
ai_generated
true
警告:数据竞争:协程X写入,协程Y读取(sync.Pool)
WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (sync.Pool)
ID: go/goroutine-sync-pool-race
80%修复率
83%置信度
0证据数
2025-09-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.3 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
未正确理解sync.Pool的使用;池中的对象可能在没有同步的情况下被并发访问。
English
Using sync.Pool without proper understanding; objects in pool may be accessed concurrently without synchronization.
解决方案
-
85% 成功率 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% 成功率 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)
无效尝试
常见但无效的做法:
-
Assuming sync.Pool provides synchronization
90% 失败
sync.Pool does not synchronize access to pooled objects; only the pool itself is safe.
-
Using global variables instead of pool
80% 失败
Global variables are shared; race condition persists.