Missing Context Cancellation (ARCH5034)
هذا المحتوى غير متوفر بلغتك بعد.
Key go.async.missing-context-cancel · Severity warning · Category async
A context created with context.WithCancel, WithTimeout, or WithDeadline returns a cancel function that must be called. When it is never called - commonly a missing defer cancel() - the context’s resources leak.
Incorrect
Section titled “Incorrect”func Run(parent context.Context) error { ctx, _ := context.WithTimeout(parent, 5*time.Second) return doWork(ctx)}Correct
Section titled “Correct”func Run(parent context.Context) error { ctx, cancel := context.WithTimeout(parent, 5*time.Second) defer cancel() return doWork(ctx)}Capture the cancel function and call defer cancel() immediately after.