go runtime_error ai_generated true

goroutine blocked in select with no ready case (deadlock or long stall)

ID: go/select-no-case-ready-block

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

A select statement with no default blocks until one of its cases can proceed. If none of the channels ever become ready, the goroutine stalls indefinitely.

generic

中文

没有 default 的 select 语句会阻塞直到某个 case 可以执行。如果所有 channel 永远不会就绪,goroutine 将无限期停滞。

Workarounds

  1. 90% success
    select {
    case v := <-ch:
    	use(v)
    case <-time.After(5 * time.Second):
    	return errors.New("timeout waiting for value")
    }
  2. 90% success
    select {
    case v := <-ch:
    	use(v)
    case <-ctx.Done():
    	return ctx.Err()
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    A default with a loop spins the CPU at 100% and does not make any channel ready.

  2. 80% fail

    If the channels are never written to, more cases do not help; the goroutine still blocks.