go runtime_error ai_generated true

fatal error: sync: 解锁未加锁的 RWMutex

fatal error: sync: RUnlock of unlocked RWMutex

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

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

版本兼容性

版本状态引入弃用备注
1.0+ active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 90% 失败

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