Skip to content

context-cancel-in-loop

Default severity: warning

context.WithCancel, WithTimeout, and WithDeadline retain parent or timer resources until cancellation. A defer in the surrounding function runs too late; cancel during the iteration or move the body into a helper.

for _, item := range items {
itemCtx, cancel := context.WithCancel(ctx)
defer cancel()
use(itemCtx, item)
}
for _, item := range items {
if err := handleItem(ctx, item); err != nil {
return err
}
}
func handleItem(ctx context.Context, item Item) error {
itemCtx, cancel := context.WithCancel(ctx)
defer cancel()
return use(itemCtx, item)
}