{"record":{"id":"1c5c2bd2db7328ab","repo":"thanos-io/thanos","slug":"closing-snappy-stream-writer","errorCode":null,"errorMessage":"closing snappy stream writer","messagePattern":"closing snappy stream writer","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/store/postings_codec.go","lineNumber":128,"sourceCode":"\t\tv := p.At()\n\t\tif v < prev {\n\t\t\treturn nil, errors.Errorf(\"postings entries must be in increasing order, current: %d, previous: %d\", v, prev)\n\t\t}\n\n\t\tuvarintSize := binary.PutUvarint(uvarintEncodeBuf, uint64(v-prev))\n\t\tif written, err := sw.Write(uvarintEncodeBuf[:uvarintSize]); err != nil {\n\t\t\treturn nil, errors.Wrap(err, \"writing uvarint encoded byte\")\n\t\t} else if written != uvarintSize {\n\t\t\treturn nil, errors.Wrap(err, \"short-write for uvarint encoded byte\")\n\t\t}\n\n\t\tprev = v\n\t}\n\tif p.Err() != nil {\n\t\treturn nil, p.Err()\n\t}\n\tif err := sw.Close(); err != nil {\n\t\treturn nil, errors.Wrap(err, \"closing snappy stream writer\")\n\t}\n\n\treturn compressedBuf.Bytes(), nil\n}\n\nfunc diffVarintSnappyStreamedDecode(input []byte, disablePooling bool) (closeablePostings, error) {\n\tif !isDiffVarintSnappyStreamedEncodedPostings(input) {\n\t\treturn nil, errors.New(\"header not found\")\n\t}\n\n\treturn newStreamedDiffVarintPostings(input[len(codecHeaderStreamedSnappy):], disablePooling)\n}\n\ntype streamedDiffVarintPostings struct {\n\tcurSeries storage.SeriesRef\n\n\terr               error\n\tinput, buf        []byte","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/store/postings_codec.go#L110-L146","documentation":"Closing the snappy stream writer after encoding all postings failed. Close flushes the final snappy compressed block into compressedBuf; if it errors, the encoded postings cache entry is incomplete and is discarded rather than cached in corrupted form.","triggerScenarios":"encodePostingsToCache -> diffVarintSnappyStreamedEncode after iterating all postings, when sw.Close() returns an error (flush/trailer write failure).","commonSituations":"Memory pressure during final flush; an already-failed underlying writer.","solutions":["Check the wrapped underlying error for root cause","Retry the cache encoding","Increase memory if the failure happens during large postings flushes"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"b, err := diffVarintSnappyStreamedEncode(p, length)\nif err != nil {\n    if strings.Contains(err.Error(), \"closing snappy stream writer\") {\n        log.WithError(errors.Unwrap(err)).Error(\"snappy stream close/flush failed\")\n        return retryEncoder(p, length)\n    }\n    return nil, err\n}","preventionTips":["Ensure memory headroom for the final flush of large postings streams","Log the unwrapped root cause","Retry the full encode; do not reuse a failed stream writer"],"tags":["snappy","encoding","close"],"backgroundTag":"broken-pipe","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}