go runtime_error ai_generated true

fatal error: sync: RUnlock of unlocked RWMutex

ID: go/mutex-unlock-of-unlocked-rwmutex

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

RUnlock() is called more times than RLock() on a sync.RWMutex, or RUnlock is called after a Lock (write lock) instead of RLock.

generic

中文

对 sync.RWMutex 调用 RUnlock() 的次数多于 RLock(),或在 Lock(写锁)之后错误地调用了 RUnlock。

Workarounds

  1. 98% success
    mu.RLock()
    defer mu.RUnlock()
    // read section
  2. 96% success
    func (c *Cache) Get(k string) (V, bool) {
        c.mu.RLock()
        defer c.mu.RUnlock()
        v, ok := c.m[k]
        return v, ok
    }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Removes read concurrency and may not fix the mismatched lock/unlock pattern.

  2. 90% fail

    Hides the bug and can leave the lock in an inconsistent state.