go runtime_error ai_generated true

fatal error: 所有 goroutine 已休眠 - 死锁!(RWMutex 读等待写)

fatal error: all goroutines are asleep - deadlock! (RWMutex reader waiting on writer)

ID: go/deadlock-rwmutex-reader-writer

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-09-12首次发现

版本兼容性

版本状态引入弃用备注
1.20 active — — —
1.21 active — — —
1.22 active — — —

根因分析

读持有 RLock 后试图获取写锁 Lock,或者递归 RLock 因已有写者排队而阻塞。

English

A reader holds RLock and tries to acquire Lock, or a recursive RLock blocks because a pending writer is queued.

generic

解决方案

  1. 90% 成功率
    Never upgrade RLock to Lock; release the read lock before acquiring the write lock:
    
    mu.RUnlock()
    mu.Lock()
    defer mu.Unlock()
  2. 88% 成功率
    Use a copy-on-write pattern with atomic.Pointer to avoid nested locks entirely.

无效尝试

常见但无效的做法:

  1. 80% 失败

    Eliminates read concurrency but the nested-lock logic still deadlocks under the same call path.

  2. 85% 失败

    The pending writer still blocks new readers; sleep merely reorders the deadlock.