The cancel function of a derived context is discarded
What it finds
ctx, _ := context.WithTimeout(parent, d): the cancel function is thrown away, so the new context, and its timer, stay alive until the parent is canceled or the deadline passes, however early the work is done.
context is resolved through the import of the file. Only the blank identifier is recognized as discarding the function; a cancel function that is kept but never called needs data flow and is not reported.
From the rule's source, lost_cancel.rs.
package p import ( "context" stdctx "context" "time") var background, _ = context.WithCancel(context.Background())tx0 reports: the cancel function of context.WithCancel is discarded func work(parent context.Context, d time.Duration) { ctx, _ := context.WithTimeout(parent, d)tx0 reports: the cancel function of context.WithTimeout is discarded ctx, _ = stdctx.WithDeadline(ctx, time.Now())tx0 reports: the cancel function of stdctx.WithDeadline is discarded ctx, cancel := context.WithTimeout(ctx, d) defer cancel() _, _ = context.WithCancel(ctx)tx0 reports: discarded _ = context.WithoutCancel(ctx) value, _ := lookup(ctx) _ = value { context := fake{} ctx, _ := context.WithTimeout(parent, d) _ = ctx }} type fake struct{} func (fake) WithTimeout(ctx context.Context, d time.Duration) (context.Context, func()) { return ctx, func() {}} func lookup(ctx context.Context) (string, bool) { return "", false } Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/go:lost-cancel" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:lost-cancel] reason="..."tx0 explain tx0/go:lost-cancel shows the same in the terminal. Found a false positive? Report it.