go runtime_error ai_generated true

fatal error: 所有 goroutine 都在休眠 - 死锁!(互斥锁加锁顺序)

fatal error: all goroutines are asleep - deadlock! (mutex lock ordering)

ID: go/deadlock-two-mutexes

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

版本兼容性

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

根因分析

两个 goroutine 以相反顺序获取互斥锁 A 和 B,各自等待对方持有的锁。

English

Two goroutines acquire mutexes A and B in opposite orders, each waiting for the other's lock.

generic

解决方案

  1. 92% 成功率
    // always lock A then B
    muA.Lock()
    defer muA.Unlock()
    muB.Lock()
    defer muB.Unlock()
  2. 88% 成功率
    var mu sync.Mutex
    mu.Lock()
    defer mu.Unlock()
    // access both A and B

无效尝试

常见但无效的做法:

  1. 70% 失败

    Busy retry burns CPU and does not guarantee progress; can still livelock.

  2. 60% 失败

    Serializes everything, destroying concurrency, and may still deadlock with other lock paths.