What version of Go are you using (go version)?
go version go1.16.5 darwin/amd64
What did you do?
go vet fails on discarded cancel function in child context, even though the context package states parent cancellation cancels all children.
Getting the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak
A very simplified example
func ParentCall(ctx context.Context) {
c, cancel := context.WithCancel(ctx)
Call(c)
// wait for asyncs
cancel()
}
func Call(ctx context.Context) {
c, _ = context.WithDeadline(ctx, getDeadline())
asyncCall(c)
// can't call `cancel()` here since the `asyncCall` hasn't finished yet.
}
What version of Go are you using (
go version)?go version go1.16.5 darwin/amd64
What did you do?
go vetfails on discarded cancel function in child context, even though thecontextpackage states parent cancellation cancels all children.Getting
the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leakA very simplified example