go runtime_error ai_generated true

致命错误:sync:解锁未加锁的 mutex

fatal error: sync: unlock of unlocked mutex

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

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

版本兼容性

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

根因分析

对一个当前未加锁的 sync.Mutex 调用 Unlock(),通常是在未执行 Lock 的错误路径上执行了 defer Unlock。

English

Unlock() is called on a sync.Mutex that is not currently locked, usually from a deferred Unlock on an error path where Lock was never reached.

generic

解决方案

  1. 95% 成功率
    mu.Lock()
    defer mu.Unlock()
    // critical section
  2. 88% 成功率
    func lock(mu *sync.Mutex) func() {
        mu.Lock()
        return mu.Unlock
    }
    unlock := lock(&mu)
    defer unlock()

无效尝试

常见但无效的做法:

  1. 75% 失败

    The mutex state is inconsistent; subsequent Lock calls may deadlock or panic unpredictably.

  2. 70% 失败

    RWMutex has the same Unlock semantics, so the bug persists.