{"record":{"id":"73ba90d593c55928","repo":"projectdiscovery/subfinder","slug":"failed-to-close-response-body-w","errorCode":null,"errorMessage":"failed to close response body: %w","messagePattern":"failed to close response body: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/subscraping/sources/pugrecon/pugrecon.go","lineNumber":86,"sourceCode":"\t\t// Prepare headers\n\t\theaders := map[string]string{\n\t\t\t\"Authorization\": \"Bearer \" + randomApiKey,\n\t\t\t\"Content-Type\":  \"application/json\",\n\t\t\t\"Accept\":        \"application/json\",\n\t\t}\n\n\t\tapiURL := \"https://pugrecon.com/api/v1/domains\"\n\t\ts.requests++\n\t\tresp, err := session.HTTPRequest(ctx, http.MethodPost, apiURL, \"\", headers, bodyReader, subscraping.BasicAuth{}) // Use HTTPRequest for full header control\n\t\tif err != nil {\n\t\t\tresults <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: err}\n\t\t\ts.errors++\n\t\t\tsession.DiscardHTTPResponse(resp)\n\t\t\treturn\n\t\t}\n\t\tdefer func() {\n\t\t\tif err := resp.Body.Close(); err != nil {\n\t\t\t\tresults <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf(\"failed to close response body: %w\", err)}\n\t\t\t\ts.errors++\n\t\t\t}\n\t\t}()\n\n\t\tif resp.StatusCode != http.StatusOK {\n\t\t\terrorMsg := fmt.Sprintf(\"received status code %d\", resp.StatusCode)\n\t\t\t// Attempt to read error message from body if possible\n\t\t\tvar apiResp pugreconAPIResponse\n\t\t\tif json.NewDecoder(resp.Body).Decode(&apiResp) == nil && apiResp.Message != \"\" {\n\t\t\t\terrorMsg = fmt.Sprintf(\"%s: %s\", errorMsg, apiResp.Message)\n\t\t\t}\n\t\t\tresults <- subscraping.Result{Source: s.Name(), Type: subscraping.Error, Error: fmt.Errorf(\"%s\", errorMsg)}\n\t\t\ts.errors++\n\t\t\treturn\n\t\t}\n\n\t\tvar response pugreconAPIResponse\n\t\terr = json.NewDecoder(resp.Body).Decode(&response)","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/projectdiscovery/subfinder/blob/7a0b91f0fac01b62c65328bd771a3560ae611d6a/pkg/subscraping/sources/pugrecon/pugrecon.go#L68-L104","documentation":"PugRecon closes the HTTP response body in a deferred func; if resp.Body.Close() returns an error, an Error result with this message is emitted. This is a defensive check — close errors on response bodies rarely indicate lost data, since the body was already fully consumed or discarded.","triggerScenarios":"The deferred resp.Body.Close() call returns non-nil after the PugRecon API POST completes — e.g. some transports/proxies report errors on close after connection resets.","commonSituations":"Connection torn down abruptly mid/close by proxy or server; custom HTTP transports returning close errors; usually benign noise alongside a real network issue.","solutions":["Check the wrapped %w cause — if it's a connection-reset, treat the response as unreliable and retry","Verify the status code handling already captured the real failure earlier in the flow","If noise-only, run the source again; the error is typically transient","Avoid custom RoundTrippers that surface spurious close errors"],"exampleFix":"// before\nif err := resp.Body.Close(); err != nil {\n\tresults <- subscraping.Result{... Error: fmt.Errorf(\"failed to close response body: %w\", err)}\n}\n// after\nif err := resp.Body.Close(); err != nil && resp.StatusCode == http.StatusOK {\n\tresults <- subscraping.Result{... Error: fmt.Errorf(\"failed to close response body: %w\", err)}\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"defer func() {\n\tif cerr := resp.Body.Close(); cerr != nil {\n\t\tlog.Printf(\"non-fatal close error: %v\", cerr) // don't fail the run for close errors\n\t}\n}()","preventionTips":["Treat close errors as non-fatal when the body was already read","Check the wrapped cause only if the status was OK and data looks suspect","Avoid custom transports that raise spurious close errors"],"tags":["go","http","resource-close"],"backgroundTag":"http-error-response","analyzedSha":"7a0b91f0fac01b62c65328bd771a3560ae611d6a","analyzedAt":"2026-09-06T23:52:02.109Z","contentChangedAt":"2026-09-06T23:52:02.109Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}