go runtime_error ai_generated true

fatal error: sync: RUnlock of unlocked RWMutex

ID: go/mutex-lock-of-locked

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

RWMutex.RUnlock() called without a matching RLock, often from a defer in a branch that never took the read lock, or from a panic path that skipped RLock.

generic

中文

RWMutex.RUnlock() 在没有匹配 RLock 的情况下被调用,常见于未获取读锁的分支中的 defer,或跳过 RLock 的 panic 路径。

Workarounds

  1. 96% success
    func read() {
        mu.RLock()
        defer mu.RUnlock()
        // read-only work
    }
  2. 94% success
    func withRLock(mu *sync.RWMutex, fn func()) {
        mu.RLock()
        defer mu.RUnlock()
        fn()
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The mismatched Unlock remains and now triggers 'unlock of unlocked mutex' instead.

  2. 85% fail

    More error-prone; panics now leave the lock held, causing deadlocks.