go runtime_error ai_generated true

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

ID: go/goroutine-context-cancel-not-checked

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2026-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.7 active
1.21 active

Root Cause

Goroutines block on channel operations after context is canceled, without checking cancellation.

generic

中文

协程在上下文被取消后阻塞在通道操作上,没有检查取消状态。

Workarounds

  1. 95% success Always include ctx.Done() in select
    select {
    case <-ch:
    case <-ctx.Done():
        return ctx.Err()
    }
  2. 90% success Use context.WithTimeout to automatically cancel
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    select {
    case <-ch:
    case <-ctx.Done():
        // timeout
    }

Dead Ends

Common approaches that don't work:

  1. Ignoring context in select 90% fail

    Context cancellation doesn't automatically unblock other channels.

  2. Using time.Sleep to wait for cancellation 80% fail

    Sleep doesn't handle cancellation; goroutine still blocks.