go runtime_error ai_generated true

致命错误:所有 goroutine 都处于休眠状态 - 死锁!(涉及 2 个通道)

fatal error: all goroutines are asleep - deadlock! (with 2 channels)

ID: go/deadlock-multiple-channels

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

版本兼容性

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

根因分析

两个 goroutine 各自等待对方发送数据的通道,导致循环等待。

English

Two goroutines each waiting on a channel that the other is supposed to send to, causing a circular wait.

generic

解决方案

  1. 90% 成功率
    ch1 := make(chan int)
    ch2 := make(chan int)
    go func() { ch1 <- 1; <-ch2 }()
    go func() { ch2 <- 2; <-ch1 }()
  2. 85% 成功率
    select {
    case v := <-ch1:
        // handle
    case <-time.After(time.Second):
        // timeout
    }

无效尝试

常见但无效的做法:

  1. 80% 失败

    Buffers only help if sends don't block; in circular wait, both block indefinitely.

  2. 60% 失败

    If both use timeouts, they may repeatedly timeout without progress.

  3. 70% 失败

    More goroutines don't resolve circular dependency; they may also block.