go
runtime_error
ai_generated
true
context canceled
ID: go/context-canceled-in-goroutine
80%Fix Rate
88%Confidence
0Evidence
2024-10-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.7+ | active | — | — | — |
Root Cause
A context was canceled (either by timeout, deadline, or explicit cancel) while a goroutine was still using it. The goroutine receives context.Canceled error from a function that respects the context.
generic中文
上下文被取消(由于超时、截止时间或显式取消),而某个 goroutine 仍在使用它。该 goroutine 从遵循上下文的函数中收到 context.Canceled 错误。
Workarounds
-
98% success
select { case <-ctx.Done(): return ctx.Err() default: // continue } -
95% success
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel()
Dead Ends
Common approaches that don't work:
-
95% fail
Ignoring cancellation can lead to resource leaks, wasted work, and violating the contract of context-aware functions.
-
90% fail
This loses the cancellation signal and any deadlines, potentially causing the goroutine to run indefinitely.