go runtime_error ai_generated true

致命错误:所有协程都处于休眠状态 - 死锁!(互斥锁)

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

ID: go/goroutine-mutex-deadlock

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

版本兼容性

版本状态引入弃用备注
1.18 active
1.19 active
1.20 active
1.21 active

根因分析

两个或多个协程持有锁并等待彼此释放,导致循环等待条件。

English

Two or more goroutines hold locks and wait for each other to release them, causing a circular wait condition.

generic

解决方案

  1. 95% 成功率 Always lock mutexes in the same order
    // Lock mu1 then mu2 consistently
    mu1.Lock()
    mu2.Lock()
    // unlock in reverse order
  2. 80% 成功率 Use trylock with a fallback
    if mu.TryLock() {
        defer mu.Unlock()
        // critical section
    } else {
        // handle contention
    }

无效尝试

常见但无效的做法:

  1. Use a single global mutex 50% 失败

    A single mutex can cause contention but not deadlock if used correctly; however, if nested locks are needed, it can still deadlock.

  2. Ignore the deadlock and restart the program 90% 失败

    Temporary fix; the deadlock will recur.