go vet "the cancel function is not used on all paths" in CI
The lostcancel analyzer detects a cancel function returned by context.WithCancel, WithTimeout, or WithDeadline that is dropped or not called on some path, which leaks the context.
What this error means
go vet reports "the cancel function is not used on all paths (possible context leak)" or "the cancel function returned by context.WithTimeout should be called, not discarded".
go vet
./fetch.go:15:2: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leakCommon causes
The cancel function is assigned to _ or ignored
Discarding cancel (or never calling it) leaves the derived context and its timer alive until the parent is cancelled.
An early return skips the cancel call
A path returns before reaching cancel(), so on that branch the context is never cancelled.
How to fix it
Defer the cancel right after creating the context
- Capture the
cancelfunction returned alongside the context. - Call
defer cancel()immediately so every path releases it. - Re-run
go vet ./....
fetch.go
ctx, cancel := context.WithTimeout(parent, 5*time.Second)
defer cancel()Call cancel explicitly on early-return paths
If you cannot defer, ensure every branch that returns calls cancel() first.
How to prevent it
- Always
defer cancel()on the line after creating the context. - Run
go vet ./...in CI so leaks are caught. - Never assign the cancel function to
_.
Related guides
go vet "Printf arg of wrong type" in CIFix go vet "Printf format %d has arg of wrong type string" in CI - a format verb does not match the type of t…
go vet "unreachable code" in CIFix go vet "unreachable code" in CI - statements follow a terminating statement such as return, panic, or an…
go vet "misuse of unbuffered os.Signal channel" in CIFix go vet "misuse of unbuffered os.Signal channel as argument to signal.Notify" in CI - signal.Notify needs…