go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (sync.Mutex.Lock on already-held lock)

ID: go/mutex-double-lock-self-deadlock

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.22 active — — —

Root Cause

The same goroutine calls Lock() twice on a non-reentrant sync.Mutex, or a locked method calls another method that locks the same mutex.

generic

中文

同一个 goroutine 对不可重入的 sync.Mutex 调用两次 Lock(),或已加锁的方法调用另一个锁定同一互斥锁的方法。

Workarounds

  1. 95% success
    Split into exported methods that lock and unexported helpers that assume the lock is held; never call Lock inside a locked section
  2. 85% success
    Use defer mu.Unlock() consistently and audit call chains for re-entrancy

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The goroutine itself holds the lock; retry never succeeds

  2. 75% fail

    RLock while holding RLock is allowed but Lock after RLock still deadlocks