go runtime_error ai_generated true

panic: sync: RUnlock of unlocked RWMutex

ID: go/rwmutex-unlock-without-lock

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

RUnlock() was called without a matching RLock(), or the mutex was locked for writing instead. Mismatched lock/unlock pairs on RWMutex panic.

generic

中文

在没有对应 RLock() 的情况下调用 RUnlock(),或该锁实际是写锁。RWMutex 上锁/解锁配对不匹配会 panic。

Workarounds

  1. 94% success
    mu.RLock()
    defer mu.RUnlock()
    // read-only access
  2. 90% success
    func (s *Store) Get(k string) (V, bool) {
        s.mu.RLock()
        defer s.mu.RUnlock()
        v, ok := s.m[k]
        return v, ok
    }

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Loses read concurrency benefits and may not fix the underlying mismatched lock/unlock logic.

  2. 88% fail

    Hides the bug; the RWMutex internal state may be inconsistent, leading to deadlocks later.