{"record":{"id":"f2580be1c5294446","repo":"crowdsecurity/crowdsec","slug":"context-must-be-non-nil","errorCode":null,"errorMessage":"context must be non-nil","messagePattern":"context must be non-nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apiclient/client_http.go","lineNumber":76,"sourceCode":"\n\treq, err := http.NewRequestWithContext(ctx, method, u.String(), buf)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif body != nil {\n\t\treq.Header.Set(\"Content-Type\", \"application/json\")\n\t\tif compressedBody {\n\t\t\treq.Header.Set(\"Content-Encoding\", \"gzip\")\n\t\t}\n\t}\n\n\treturn req, nil\n}\n\nfunc (c *ApiClient) Do(ctx context.Context, req *http.Request, v any) (*Response, error) {\n\tif ctx == nil {\n\t\treturn nil, errors.New(\"context must be non-nil\")\n\t}\n\n\treq = req.WithContext(ctx)\n\n\t// Check rate limit\n\n\tif c.UserAgent != \"\" {\n\t\treq.Header.Add(\"User-Agent\", c.UserAgent)\n\t}\n\n\tlog.Debugf(\"[URL] %s %s\", req.Method, req.URL)\n\n\tresp, err := c.client.Do(req)\n\tif resp != nil && resp.Body != nil {\n\t\tdefer resp.Body.Close()\n\t}\n\n\tif err != nil {","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/apiclient/client_http.go#L58-L94","documentation":"ApiClient.Do requires a context to attach to the outgoing HTTP request via req.WithContext(ctx). A nil context would make cancellation, deadlines and trace propagation impossible, so the client rejects it up front instead of failing obscurely inside net/http.","triggerScenarios":"Calling Do(ctx, req, v) with a literally nil context.Context value, or with a variable that was declared as context.Context without initialization (zero value of an interface).","commonSituations":"Older code written before context was pervasive, wrappers that pass through a nil ctx field of a struct, tests that call Do(nil, ...) directly, refactors where a caller dropped ctx argument handling.","solutions":["Pass a valid context: use context.Background() or context.TODO() when no request-scoped context exists.","Pass the ctx received from an HTTP handler, command, or parent function instead of a stored nil field.","Add a nil-check/default at the call site wrapper before invoking Do."],"exampleFix":"// before\nvar ctx context.Context\nresp, err := client.Do(ctx, req, &result)\n// after\nctx := context.Background()\nresp, err := client.Do(ctx, req, &result)","handlingStrategy":"validation","validationCode":"if ctx == nil {\n    ctx = context.Background()\n}\nresp, err := client.Do(ctx, req, &result)","typeGuard":"func validCtx(ctx context.Context) context.Context {\n    if ctx == nil {\n        return context.Background()\n    }\n    return ctx\n}","tryCatchPattern":"resp, err := client.Do(ctx, req, &out)\nif err != nil {\n    return fmt.Errorf(\"api call: %w\", err)\n}","preventionTips":["Never declare a context.Context without initializing it; always assign context.Background/TODO or a parent ctx.","Lint for nil-context params in HTTP client wrappers.","Thread ctx through function signatures instead of storing it in structs."],"tags":["http","context","api-client"],"backgroundTag":"null-argument","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}