{"record":{"id":"7f864c5ba80ad08f","repo":"crowdsecurity/crowdsec","slug":"writing-to-gzip-writer-w","errorCode":null,"errorMessage":"writing to gzip writer: %w","messagePattern":"writing to gzip writer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/apiclient/client_http.go","lineNumber":49,"sourceCode":"\tvar buf io.ReadWriter\n\tcompressedBody := false\n\n\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\")","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/apiclient/client_http.go#L31-L67","documentation":"PrepareRequest gzips JSON bodies larger than 5KB and wraps any failure of the compressed write. gzip.Writer.Write only fails when the underlying bytes.Buffer write fails or the writer is in an error state — practically this is nearly impossible with an in-memory buffer, so this error indicates an unexpected internal failure during body compression.","triggerScenarios":"len(jsonBytes) > 5*1024 and gzipWriter.Write(jsonBytes) returns a non-nil error — e.g. an in-memory allocation failure or a writer corrupted by prior misuse (here a fresh writer, so essentially only OOM/resource-exhaustion conditions).","commonSituations":"Host under extreme memory pressure while serializing large alert/signal batches; custom instrumented builds that alter gzip behavior; otherwise effectively unreachable in normal operation.","solutions":["Check host memory availability if this recurs — the in-memory gzip write failing usually means resource exhaustion","Retry the operation; if transient, a fresh call will succeed","Report to maintainers with the inner error if it reproduces reliably on a healthy host"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < 3; attempt++ {\n    req, err := client.PrepareRequest(ctx, method, u, body)\n    if err == nil {\n        break\n    }\n    if strings.Contains(err.Error(), \"gzip\") {\n        time.Sleep(time.Second) // transient compression/resource failure\n        continue\n    }\n    return err\n}","preventionTips":["Ensure hosts sending large signal batches have adequate memory","Keep individual request payloads reasonably sized","Treat gzip failures as infrastructural, not input problems"],"tags":["gzip","compression","api-client"],"backgroundTag":"payload-too-large","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"}