A function that is given a context starts a new one with context.Background() or context.TODO()
What it finds
func (s *Store) Load(ctx context.Context) { ... s.db.QueryContext(context.Background(), q) }: the new context does not end when the caller's does, so cancellation and deadlines stop at this function and the work goes on after the caller gave up. The context parameter was usually meant.
The function must have a parameter of type context.Context that is not the blank identifier, and call Background or TODO of context in its own body or in a function literal inside it. Work that must outlive the caller, such as cleanup after a cancellation, is a deliberate use; context.WithoutCancel(ctx) says so and keeps the values of the context.
From the rule's source, fresh_context_with_parameter.rs.
package p import ( "context" "database/sql") func load(ctx context.Context, db *sql.DB) error { _, err := db.QueryContext(context.Background(), "SELECT 1")tx0 reports: context.Background() ignores the context parameter ctx go func() { _ = db.PingContext(context.TODO())tx0 reports: context.TODO() ignores the context parameter ctx }() return err} func start(db *sql.DB) error { return db.PingContext(context.Background())} func handler(_ context.Context, db *sql.DB) error { return db.PingContext(context.Background())} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:fresh-context-with-parameter" = "warning"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:fresh-context-with-parameter] reason="..."tx0 explain tx0/go:fresh-context-with-parameter shows the same in the terminal. Found a false positive? Report it.