go runtime_error ai_generated true

error: context canceled

ID: go/context-canceled-propagated

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21+ active — — —

Root Cause

A parent context was canceled (or timed out) and child operations returned context.Canceled. Often caused by deferring cancel() too early or sharing a request context across background work.

generic

中文

父 context 被取消(或超时),子操作返回 context.Canceled。常见原因是过早 defer cancel(),或把请求 context 复用到后台任务。

Workarounds

  1. 90% success
    bg := context.WithoutCancel(ctx)
    go func() { longRunning(bg) }()
  2. 92% success
    if errors.Is(err, context.Canceled) {
        return nil // expected shutdown
    }

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Hides real shutdown bugs and leaks resources; also masks timeout-induced failures.

  2. 70% fail

    Breaks request cancellation and leaks goroutines after client disconnect.