Ir al contenido

Missing Context Cancellation (ARCH5034)

Esta página aún no está disponible en tu idioma.

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.

func Run(parent context.Context) error {
ctx, _ := context.WithTimeout(parent, 5*time.Second)
return doWork(ctx)
}
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.