go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/goroutine-leak-on-missed-channel

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

A goroutine sends to an unbuffered channel but no receiver ever reads, causing the goroutine to block forever. The main goroutine also waits on another operation, leading to a deadlock.

generic

中文

一个协程向无缓冲通道发送数据,但没有接收者读取,导致协程永久阻塞。主协程也在等待其他操作,引发死锁。

Workarounds

  1. 70% success
    select { case ch <- v: default: /* 处理超时 */ }
  2. 90% success
    ch := make(chan int, 10); go func(){ v := <-ch; /* 处理 */ }(); ch <- 42

Dead Ends

Common approaches that don't work:

  1. 80% fail

    缓冲只能容纳一个值,如果接收者仍然缺失,发送第二个值时会再次阻塞。

  2. 90% fail

    sleep只是暂时缓解,如果接收者一直不出现,最终仍会死锁。