go runtime_error ai_generated true

panic: context canceled (in goroutine)

ID: go/context-cancel-not-propagated

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.22 active

Root Cause

A context is canceled, but a goroutine doesn't check the context and tries to use it, causing a panic or error.

generic

中文

上下文被取消,但 goroutine 未检查上下文并尝试使用它,导致恐慌或错误。

Workarounds

  1. 90% success
    Check ctx.Err() before and after operations, and return early if canceled.
  2. 95% success
    Pass ctx to all functions and use select with ctx.Done() to handle cancellation.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    May lead to resource leaks or incorrect results; cancellation should be respected.

  2. 60% fail

    Loses cancellation ability; may cause indefinite blocking.