go
runtime_error
ai_generated
true
恐慌:同步:对未锁定的互斥锁解锁
panic: sync: unlock of unlocked mutex
ID: go/mutex-unlock-not-locked
80%修复率
90%置信度
0证据数
2024-05-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
在当前协程未锁定的 sync.Mutex 上调用 Unlock,通常由于缺少 Lock 或重复 Unlock。
English
Calling Unlock on a sync.Mutex that is not locked by the current goroutine, often due to missing Lock or double Unlock.
解决方案
-
99% 成功率 Always pair Lock with defer Unlock immediately
mu.Lock(); defer mu.Unlock(); // critical section
-
98% 成功率 Use sync.RWMutex with proper RLock/RUnlock pairing
mu.RLock(); defer mu.RUnlock(); // read section
无效尝试
常见但无效的做法:
-
Adding defer mu.Unlock() without ensuring Lock is called
70% 失败
If Lock panics, Unlock still called on unlocked mutex.
-
Using recover to catch panic and continue
50% 失败
Does not fix root cause; logic may be inconsistent.