go
runtime_error
ai_generated
true
fatal error: sync: unlock of unlocked mutex
ID: go/mutex-unlock-of-unlocked
80%Fix Rate
90%Confidence
0Evidence
2024-06-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.x | active | — | — | — |
Root Cause
Unlock() is called on a sync.Mutex that is not currently locked by this goroutine. Often caused by double Unlock, or Unlock in a defer that runs after an early return path already unlocked.
generic中文
对当前未由本 goroutine 锁定的 sync.Mutex 调用 Unlock()。常由重复 Unlock 引起,或 defer 中的 Unlock 在提前返回路径已解锁后再次执行。
Workarounds
-
95% success
Use defer mu.Unlock() immediately after a single mu.Lock() and never unlock elsewhere: mu.Lock() defer mu.Unlock() // critical section
-
85% success
Encapsulate lock/unlock in a helper that tracks state, or restructure so the lock scope matches the function scope exactly.
Dead Ends
Common approaches that don't work:
-
95% fail
This is a fatal runtime error, not a recoverable panic; recover does not catch it.
-
70% fail
Increases risk of forgetting to unlock on error paths, causing deadlocks instead.