go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (mutex lock ordering)

ID: go/deadlock-two-mutexes

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

Two goroutines acquire mutexes A and B in opposite orders, each waiting for the other's lock.

generic

中文

两个 goroutine 以相反顺序获取互斥锁 A 和 B,各自等待对方持有的锁。

Workarounds

  1. 92% success
    // always lock A then B
    muA.Lock()
    defer muA.Unlock()
    muB.Lock()
    defer muB.Unlock()
  2. 88% success
    var mu sync.Mutex
    mu.Lock()
    defer mu.Unlock()
    // access both A and B

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Busy retry burns CPU and does not guarantee progress; can still livelock.

  2. 60% fail

    Serializes everything, destroying concurrency, and may still deadlock with other lock paths.