go
runtime_error
ai_generated
true
恐慌:sync: Mutex 已锁定(复制了 sync.Mutex 的值)
panic: sync: Mutex is locked (copied value of sync.Mutex)
ID: go/mutex-copy-after-use
80%修复率
87%置信度
0证据数
2024-02-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.19 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
sync.Mutex 在使用后被复制,导致未定义行为,并因 vet 或运行时检测而恐慌。
English
A sync.Mutex is copied after being used, which leads to undefined behavior and panic due to vet or runtime detection.
解决方案
-
95% 成功率
Always use pointer to the struct that holds the mutex, never copy it. Example: func (m *MyStruct) Lock()
-
90% 成功率
Use sync.Mutex as a field and embed it in a struct that is always used via pointer.
无效尝试
常见但无效的做法:
-
90% 失败
Undefined behavior can corrupt data or crash later; not safe.
-
80% 失败
Copying the struct still copies the mutex; pointer receiver alone doesn't help.