go runtime_error ai_generated true

fatal error: sync: unlock of unlocked mutex

ID: go/mutex-unlock-of-unlocked

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.14+ active

Root Cause

mu.Unlock() is called without a matching Lock(), often from a deferred Unlock in a path where Lock was skipped or Unlock ran twice.

generic

中文

在没有匹配 Lock() 的情况下调用 mu.Unlock(),通常发生在 Lock 被跳过的路径中 defer Unlock,或 Unlock 执行了两次。

Workarounds

  1. 94% success
    mu.Lock()
    defer mu.Unlock()
    // critical section
  2. 90% success
    func (s *Store) Get(k string) V {
        s.mu.Lock()
        defer s.mu.Unlock()
        return s.m[k]
    }

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Swallowing the fatal error hides the logic bug and can leave the mutex in an inconsistent state.

  2. 80% fail

    TryLock is not a validity check for Unlock and adds races; the double-unlock still occurs.