go
runtime_error
ai_generated
true
致命错误:所有 goroutine 处于休眠状态 - 死锁! goroutine 1 [sync.Mutex.Lock]:
fatal error: all goroutines are asleep - deadlock! goroutine 1 [sync.Mutex.Lock]:
ID: go/deadlock-mutex-double-lock
80%修复率
90%置信度
0证据数
2024-02-27首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
一个 goroutine 在未解锁的情况下两次锁定同一个 sync.Mutex,或两个 goroutine 以相反顺序获取两个互斥锁。运行时检测到完全停止。
English
A goroutine locks a sync.Mutex twice without unlocking in between, or two goroutines acquire two mutexes in opposite order. The runtime detects a full stop.
解决方案
-
96% 成功率
func (s *Service) Do() { s.mu.Lock() defer s.mu.Unlock() // ... } -
93% 成功率
// Always lock A before B func transfer(a, b *Account) { if a.id < b.id { a.mu.Lock(); b.mu.Lock() } else { b.mu.Lock(); a.mu.Lock() } defer a.mu.Unlock(); defer b.mu.Unlock() }
无效尝试
常见但无效的做法:
-
90% 失败
sync.Mutex has no timeout API; you must restructure the code or use a channel-based lock.
-
95% 失败
All runnable goroutines are blocked on the mutex; more Ps do not help.