go runtime_error ai_generated true

fatal error: sync: unlock of unlocked mutex

ID: go/mutex-not-unlocked

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

Attempting to unlock a mutex that is not locked, often due to double unlock or unlocking in wrong goroutine.

generic

中文

尝试解锁一个未锁定的互斥锁,通常是由于重复解锁或在错误的 goroutine 中解锁。

Workarounds

  1. 95% success Always use defer to unlock immediately after lock
    mu.Lock()
    defer mu.Unlock()
    // critical section
  2. 90% success Ensure unlock is called exactly once per lock
    Use paired Lock/Unlock in same function; avoid manual unlock in error paths without defer.

Dead Ends

Common approaches that don't work:

  1. Adding a flag to check if mutex is locked 85% fail

    Mutex does not expose lock state; race conditions on the flag.

  2. Using recover() to ignore the panic 70% fail

    Recovering does not fix the logic error; may cause data corruption.