go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (sync.Mutex lock ordering)

ID: go/goroutine-mutex-lock-deadlock

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active
1.21 active
1.22 active

Root Cause

Two or more goroutines hold mutexes and wait for each other to release, creating a circular dependency.

generic

中文

两个或多个goroutine持有互斥锁并等待对方释放,形成循环依赖。

Workarounds

  1. 95% success Always acquire locks in a consistent global order
    // Lock mu1 then mu2; goroutine1: mu1.Lock(); mu2.Lock(); goroutine2: mu1.Lock(); mu2.Lock()
  2. 80% success Use try-lock pattern with channels
    select { case <-mu: default: // avoid deadlock }

Dead Ends

Common approaches that don't work:

  1. Using sync.RWMutex instead of Mutex 90% fail

    Does not fix lock ordering issue.

  2. Adding sleep to break deadlock 95% fail

    Sleep does not resolve logical cycle.