go
runtime_error
ai_generated
true
error: context canceled
ID: go/context-canceled-propagated
80%Fix Rate
86%Confidence
0Evidence
2024-09-04First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
90% success
bg := context.WithoutCancel(ctx) go func() { longRunning(bg) }() -
92% success
if errors.Is(err, context.Canceled) { return nil // expected shutdown }
Dead Ends
Common approaches that don't work:
-
60% fail
Hides real shutdown bugs and leaks resources; also masks timeout-induced failures.
-
70% fail
Breaks request cancellation and leaks goroutines after client disconnect.