{"record":{"id":"c1df3e44b8a0e843","repo":"crowdsecurity/crowdsec","slug":"closing-gzip-writer-w","errorCode":null,"errorMessage":"closing gzip writer: %w","messagePattern":"closing gzip writer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apiclient/client_http.go","lineNumber":52,"sourceCode":"\tif body != nil {\n\t\tjsonBuf := &bytes.Buffer{}\n\t\tenc := json.NewEncoder(jsonBuf)\n\t\tenc.SetEscapeHTML(false)\n\n\t\tif err = enc.Encode(body); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tjsonBytes := jsonBuf.Bytes()\n\t\tif len(jsonBytes) > compressionMinSize {\n\t\t\tcompressedBody = true\n\t\t\tbuf = &bytes.Buffer{}\n\t\t\tgzipWriter := gzip.NewWriter(buf)\n\t\t\tif _, err = gzipWriter.Write(jsonBytes); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"writing to gzip writer: %w\", err)\n\t\t\t}\n\t\t\tif err = gzipWriter.Close(); err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"closing gzip writer: %w\", err)\n\t\t\t}\n\t\t} else {\n\t\t\tbuf = jsonBuf\n\t\t}\n\t}\n\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","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/apiclient/client_http.go#L34-L70","documentation":"PrepareRequest wraps the error from gzipWriter.Close(), which flushes the gzip footer/trailer to the buffer. Close returns an error only if the pending write to the underlying bytes.Buffer fails, so like its sibling this points at in-memory compression failing unexpectedly rather than any user-fixable input problem.","triggerScenarios":"len(jsonBytes) > 5KB, Write succeeded, but gzipWriter.Close() returns non-nil — pending buffered data could not be written to the in-memory buffer (resource exhaustion) or the writer was already in an error state.","commonSituations":"Severe memory pressure on the machine; otherwise practically unreachable — this is a defensive wrap around the gzip writer contract.","solutions":["Free up host memory / check for OOM conditions and retry","If it persists on a healthy host, capture the inner error and report it to the crowdsec maintainers","As a workaround, reduce batch size so payloads stay under the 5KB compression threshold"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"req, err := client.PrepareRequest(ctx, method, u, body)\nif err != nil && strings.Contains(err.Error(), \"gzip\") {\n    // retry once after a short delay; failure is resource-related, not input-related\n    time.Sleep(500 * time.Millisecond)\n    req, err = client.PrepareRequest(ctx, method, u, body)\n}\nreturn req, err","preventionTips":["Monitor host memory; gzip close failures indicate resource exhaustion","Keep payloads under the compression threshold when batching is controllable","Escalate persistent occurrences as library/infra bugs"],"tags":["gzip","compression","api-client"],"backgroundTag":"gzip-stream-failed","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"}