An outgoing request is made without the context the function was given

tx0/go:request-without-contextrecommendedwarningreliability/api-misusedepth fastbug

What it finds

req, err := http.NewRequest("GET", url, nil) in func fetch(ctx context.Context, ...): the request does not carry the caller's context, so cancelling it or reaching its deadline does not stop the request, which keeps going until the server answers. http.NewRequestWithContext(ctx, ...) ties the two together.

Reported is a call of http.NewRequest, http.Get, http.Head, http.Post or http.PostForm in a function, or a function literal inside one, that has a parameter of type context.Context with a name other than _, unless the function attaches a context later with a method WithContext.

From the rule's source, request_without_context.rs.

Example from the rule's tests
package p import (	"context"	"net/http") func fetch(ctx context.Context, url string) (*http.Request, error) {	return http.NewRequest("GET", url, nil)tx0 reports: http.NewRequest ignores the context of the function} func fetchWith(ctx context.Context, url string) (*http.Request, error) {	return http.NewRequestWithContext(ctx, "GET", url, nil)} func plain(url string) (*http.Request, error) {	return http.NewRequest("GET", url, nil)}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/go:request-without-context" = "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:request-without-context] reason="..."

tx0 explain tx0/go:request-without-context shows the same in the terminal. Found a false positive? Report it.