go runtime_error ai_generated true

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

ID: go/deadlock-rwmutex-reader-writer

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 85% fail

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