Pattern: Failure to call the cancelation function returned by context.WithCancel
Issue: -
The cancelation function returned by context.WithCancel
, WithTimeout
, and WithDeadline
must be called or the new context will remain live until its parent context is canceled.
Example of incorrect code:
func _() {
var ctx, cancel = context.WithCancel()
// this return statement may be reached without using the cancel var
}
Example of correct code:
func _() {
ctx, cancel := context.WithCancel()
defer cancel()
}