go
runtime_error
ai_generated
true
fatal error: sync: 解锁未加锁的互斥锁
fatal error: sync: unlock of unlocked mutex
ID: go/fatal-error-sync-unlock-of-unlocked-mutex
80%修复率
89%置信度
0证据数
2024-07-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
在互斥锁未被持有的情况下调用 mu.Unlock(),通常因为提前返回后 defer unlock 仍执行,或重复解锁。
English
mu.Unlock() is called when the mutex is not held, usually because a defer unlock runs after an early return or a double unlock.
解决方案
-
95% 成功率
Use defer mu.Unlock() immediately after mu.Lock() in the same function so they always pair.
-
90% 成功率
Audit all code paths: ensure no early return between Lock and Unlock without defer, and never unlock in a different function than the one that locked.
无效尝试
常见但无效的做法:
-
98% 失败
This is a fatal runtime error, not a panic; recover cannot catch it and the process exits.
-
80% 失败
The flag itself needs synchronization and is often set incorrectly, reintroducing the bug under concurrency.