go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (mutex)
ID: go/goroutine-mutex-deadlock
80%Fix Rate
85%Confidence
0Evidence
2024-07-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success Always lock mutexes in the same order
// Lock mu1 then mu2 consistently mu1.Lock() mu2.Lock() // unlock in reverse order
-
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:
-
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.
-
Ignore the deadlock and restart the program
90% fail
Temporary fix; the deadlock will recur.