go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (RWMutex write lock)

ID: go/rwmutex-write-lock-deadlock

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

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.

generic

中文

持有读锁的 goroutine 尝试获取写锁,导致死锁,因为写锁需要等待所有读锁释放。

Workarounds

  1. 95% success Avoid holding read lock when acquiring write lock; release read lock first
    rw.RUnlock()
    rw.Lock()
    // write
    rw.Unlock()
    rw.RLock() // if needed again
  2. 90% success Use a single mutex instead of RWMutex if write locks are frequent
    var mu sync.Mutex
    mu.Lock()
    // write
    mu.Unlock()

Dead Ends

Common approaches that don't work:

  1. Using a separate mutex for writes 80% fail

    Does not address the recursive locking issue; may still deadlock.

  2. Increasing the number of goroutines 90% fail

    More goroutines can exacerbate the deadlock.