go runtime_error ai_generated true

panic:sync:解锁未加读锁的 RWMutex

panic: sync: RUnlock of unlocked RWMutex

ID: go/rwmutex-unlock-without-lock

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-06-15首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

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

  2. 88% 失败

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