go runtime_error ai_generated true

致命错误:所有 goroutine 处于休眠状态 - 死锁! goroutine 1 [sync.Mutex.Lock]:

fatal error: all goroutines are asleep - deadlock! goroutine 1 [sync.Mutex.Lock]:

ID: go/deadlock-mutex-double-lock

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

版本兼容性

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

根因分析

一个 goroutine 在未解锁的情况下两次锁定同一个 sync.Mutex,或两个 goroutine 以相反顺序获取两个互斥锁。运行时检测到完全停止。

English

A goroutine locks a sync.Mutex twice without unlocking in between, or two goroutines acquire two mutexes in opposite order. The runtime detects a full stop.

generic

解决方案

  1. 96% 成功率
    func (s *Service) Do() {
        s.mu.Lock()
        defer s.mu.Unlock()
        // ...
    }
  2. 93% 成功率
    // Always lock A before B
    func transfer(a, b *Account) {
        if a.id < b.id { a.mu.Lock(); b.mu.Lock() } else { b.mu.Lock(); a.mu.Lock() }
        defer a.mu.Unlock(); defer b.mu.Unlock()
    }

无效尝试

常见但无效的做法:

  1. 90% 失败

    sync.Mutex has no timeout API; you must restructure the code or use a channel-based lock.

  2. 95% 失败

    All runnable goroutines are blocked on the mutex; more Ps do not help.