go runtime_error ai_generated true

fatal error: sync: unlock of unlocked mutex

ID: go/sync-unlock-of-unlocked-mutex

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

mu.Unlock() is called without a matching Lock(), often due to a double unlock or an early Unlock in a defer plus explicit Unlock.

generic

中文

在没有对应 Lock() 的情况下调用 mu.Unlock(),常因双重解锁或 defer Unlock 与显式 Unlock 叠加。

Workarounds

  1. 95% success
    mu.Lock()
    defer mu.Unlock()
    // no other Unlock calls
  2. 93% success
    func (s *Store) withLock(fn func()) {
        s.mu.Lock()
        defer s.mu.Unlock()
        fn()
    }

Dead Ends

Common approaches that don't work:

  1. 100% fail

    This is a fatal error; recover cannot catch it.

  2. 75% fail

    The flag itself needs synchronization and often races.