go runtime_error ai_generated true

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

fatal error: sync: RUnlock of unlocked RWMutex

ID: go/mutex-lock-of-locked

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

版本兼容性

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

根因分析

RWMutex.RUnlock() 在没有匹配 RLock 的情况下被调用,常见于未获取读锁的分支中的 defer,或跳过 RLock 的 panic 路径。

English

RWMutex.RUnlock() called without a matching RLock, often from a defer in a branch that never took the read lock, or from a panic path that skipped RLock.

generic

解决方案

  1. 96% 成功率
    func read() {
        mu.RLock()
        defer mu.RUnlock()
        // read-only work
    }
  2. 94% 成功率
    func withRLock(mu *sync.RWMutex, fn func()) {
        mu.RLock()
        defer mu.RUnlock()
        fn()
    }

无效尝试

常见但无效的做法:

  1. 90% 失败

    The mismatched Unlock remains and now triggers 'unlock of unlocked mutex' instead.

  2. 85% 失败

    More error-prone; panics now leave the lock held, causing deadlocks.