go runtime_error ai_generated true

fatal error: sync: RUnlock of unlocked RWMutex

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

RWMutex.RUnlock() is called without a matching RLock(). Often due to mismatched Lock/RLock in different code paths.

generic

中文

在没有对应 RLock() 的情况下调用 RWMutex.RUnlock()。常因不同代码路径中 Lock/RLock 不匹配。

Workarounds

  1. 95% success
    func (s *Store) Get(k string) (int, bool) {
        s.mu.RLock()
        defer s.mu.RUnlock()
        v, ok := s.m[k]
        return v, ok
    }
  2. 90% success
    func (s *Store) read(fn func()) {
        s.mu.RLock()
        defer s.mu.RUnlock()
        fn()
    }

Dead Ends

Common approaches that don't work:

  1. 65% fail

    Loses read concurrency and does not fix the mismatch if the bug is elsewhere.

  2. 100% fail

    Fatal error; not recoverable.