go runtime_error ai_generated true

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

ID: go/deadlock-multiple-channels

Also available as: JSON · Markdown · 中文
80%Fix Rate
81%Confidence
0Evidence
2024-04-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 60% fail

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

  3. 70% fail

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