Skip to content

Files

Latest commit

 

History

History
31 lines (21 loc) · 770 Bytes

lostcancel.md

File metadata and controls

31 lines (21 loc) · 770 Bytes

Pattern: Failure to call the cancelation function returned by context.WithCancel

Issue: -

Description

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()
}

Further Reading