Go

Go

Made by DeepSource

Issues with cancel func returned by context.WithCancel VET-V0011

Bug risk
Major

The cancellation function returned by context.WithCancel, WithTimeout, and WithDeadline must be called or the new context will remain live until its parent context is cancelled. (The background context is never cancelled.)

This is usually an issue either because the variable was assigned to the blank identifier, or because there exists a control-flow path from the call to a return statement and that path does not "use" the cancel function.

For example, cancel is not used on all paths below, hence, is an issue.

func f(cond bool) {
    var _, cancel = context.WithCancel(bg)
    if cond {
        x = cancel
    }
}