go
runtime_error
ai_generated
true
致命错误:所有 goroutine 都处于休眠状态 - 死锁!(RWMutex 写锁)
fatal error: all goroutines are asleep - deadlock! (RWMutex write lock)
ID: go/rwmutex-write-lock-deadlock
80%修复率
85%置信度
0证据数
2024-07-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
持有读锁的 goroutine 尝试获取写锁,导致死锁,因为写锁需要等待所有读锁释放。
English
A goroutine holding a read lock tries to acquire a write lock, causing a deadlock because write lock waits for all read locks to release.
解决方案
-
95% 成功率 Avoid holding read lock when acquiring write lock; release read lock first
rw.RUnlock() rw.Lock() // write rw.Unlock() rw.RLock() // if needed again
-
90% 成功率 Use a single mutex instead of RWMutex if write locks are frequent
var mu sync.Mutex mu.Lock() // write mu.Unlock()
无效尝试
常见但无效的做法:
-
Using a separate mutex for writes
80% 失败
Does not address the recursive locking issue; may still deadlock.
-
Increasing the number of goroutines
90% 失败
More goroutines can exacerbate the deadlock.