Zum Inhalt springen

Missing Context Cancellation (ARCH5034)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.