go runtime_error ai_generated true

panic: sync: unlock of unlocked mutex

ID: go/mutex-unlock-not-locked

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-05-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 99% success Always pair Lock with defer Unlock immediately
    mu.Lock(); defer mu.Unlock(); // critical section
  2. 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:

  1. Adding defer mu.Unlock() without ensuring Lock is called 70% fail

    If Lock panics, Unlock still called on unlocked mutex.

  2. Using recover to catch panic and continue 50% fail

    Does not fix root cause; logic may be inconsistent.