go runtime_error ai_generated true

fatal error: sync: Unlock of unlocked mutex

ID: go/goroutine-mutex-not-unlocked

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

Calling Unlock on a mutex that wasn't locked by the same goroutine, often due to missing Lock or double Unlock.

generic

中文

对未被同一协程锁定的互斥锁调用Unlock,通常由于缺少Lock或重复Unlock。

Workarounds

  1. 99% success Use defer to ensure Unlock is called exactly once after Lock
    mu.Lock()
    defer mu.Unlock()
    // critical section
  2. 90% success Use sync.Mutex with proper pairing in all code paths
    mu.Lock()
    if condition {
        mu.Unlock()
        return
    }
    mu.Unlock()

Dead Ends

Common approaches that don't work:

  1. Using recover() to catch panic 100% fail

    Panic is fatal and cannot be recovered in Go runtime; program crashes.

  2. Adding more Unlock calls to match Lock 90% fail

    Double unlock causes panic; correct pairing is required.