go
runtime_error
ai_generated
true
panic: sync: unlock of unlocked mutex
ID: go/mutex-unlock-not-locked
80%Fix Rate
90%Confidence
0Evidence
2024-05-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
Calling Unlock on a sync.Mutex that is not locked by the current goroutine, often due to missing Lock or double Unlock.
generic中文
在当前协程未锁定的 sync.Mutex 上调用 Unlock,通常由于缺少 Lock 或重复 Unlock。
Workarounds
-
99% success Always pair Lock with defer Unlock immediately
mu.Lock(); defer mu.Unlock(); // critical section
-
98% success Use sync.RWMutex with proper RLock/RUnlock pairing
mu.RLock(); defer mu.RUnlock(); // read section
Dead Ends
Common approaches that don't work:
-
Adding defer mu.Unlock() without ensuring Lock is called
70% fail
If Lock panics, Unlock still called on unlocked mutex.
-
Using recover to catch panic and continue
50% fail
Does not fix root cause; logic may be inconsistent.