go
runtime_error
ai_generated
true
fatal error: sync: unlock of unlocked mutex
ID: go/fatal-error-sync-unlock-of-unlocked-mutex
80%Fix Rate
89%Confidence
0Evidence
2024-07-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
mu.Unlock() is called when the mutex is not held, usually because a defer unlock runs after an early return or a double unlock.
generic中文
在互斥锁未被持有的情况下调用 mu.Unlock(),通常因为提前返回后 defer unlock 仍执行,或重复解锁。
Workarounds
-
95% success
Use defer mu.Unlock() immediately after mu.Lock() in the same function so they always pair.
-
90% success
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.
Dead Ends
Common approaches that don't work:
-
98% fail
This is a fatal runtime error, not a panic; recover cannot catch it and the process exits.
-
80% fail
The flag itself needs synchronization and is often set incorrectly, reintroducing the bug under concurrency.