go runtime_error ai_generated true

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

ID: go/goroutine-mutex-deadlock

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active
1.21 active

Root Cause

Two or more goroutines hold locks and wait for each other to release them, causing a circular wait condition.

generic

中文

两个或多个协程持有锁并等待彼此释放,导致循环等待条件。

Workarounds

  1. 95% success Always lock mutexes in the same order
    // Lock mu1 then mu2 consistently
    mu1.Lock()
    mu2.Lock()
    // unlock in reverse order
  2. 80% success Use trylock with a fallback
    if mu.TryLock() {
        defer mu.Unlock()
        // critical section
    } else {
        // handle contention
    }

Dead Ends

Common approaches that don't work:

  1. Use a single global mutex 50% fail

    A single mutex can cause contention but not deadlock if used correctly; however, if nested locks are needed, it can still deadlock.

  2. Ignore the deadlock and restart the program 90% fail

    Temporary fix; the deadlock will recur.