{"record":{"id":"69d6bc861d572e1b","repo":"nats-io/nats-server","slug":"error-writing-checksum-w","errorCode":null,"errorMessage":"error writing checksum: %w","messagePattern":"error writing checksum: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/filestore.go","lineNumber":14531,"sourceCode":"\t}\n\n\tinput := bytes.NewReader(buf[:bodyLen])\n\tchecksum := buf[bodyLen:]\n\n\t// Compress the block content, but don't compress the checksum.\n\t// We will preserve it at the end of the block as-is.\n\tif n, err := io.CopyN(writer, input, bodyLen); err != nil {\n\t\treturn nil, fmt.Errorf(\"error writing to compression writer: %w\", err)\n\t} else if n != bodyLen {\n\t\treturn nil, fmt.Errorf(\"short write on body (%d != %d)\", n, bodyLen)\n\t}\n\tif err := writer.Close(); err != nil {\n\t\treturn nil, fmt.Errorf(\"error closing compression writer: %w\", err)\n\t}\n\n\t// Now add the checksum back onto the end of the block.\n\tif n, err := output.Write(checksum); err != nil {\n\t\treturn nil, fmt.Errorf(\"error writing checksum: %w\", err)\n\t} else if n != checksumSize {\n\t\treturn nil, fmt.Errorf(\"short write on checksum (%d != %d)\", n, checksumSize)\n\t}\n\n\treturn output.Bytes(), nil\n}\n\nfunc (alg StoreCompression) Decompress(buf []byte) ([]byte, error) {\n\tif len(buf) < checksumSize {\n\t\treturn nil, fmt.Errorf(\"compressed buffer is too short\")\n\t}\n\tbodyLen := int64(len(buf) - checksumSize)\n\tinput := bytes.NewReader(buf[:bodyLen])\n\n\tvar reader io.ReadCloser\n\tswitch alg {\n\tcase NoCompression:\n\t\treturn buf, nil","sourceCodeStart":14513,"sourceCodeEnd":14549,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/filestore.go#L14513-L14549","documentation":"Returned when the checksum cannot be written back onto the end of the compressed block. After closing the compression writer, the library appends the uncompressed trailing checksum bytes to the output buffer; a Write error means the block cannot be completed correctly. The cause is wrapped with %w.","triggerScenarios":"output.Write(checksum) returns err != nil right after writer.Close() in the StoreCompression compress function, typically because the underlying output buffer's writer errored.","commonSituations":"Disk-full on file-backed output, a failed/short underlying byte slice writer, or a custom io.Writer implementation that errors under memory pressure.","solutions":["Check the wrapped error via errors.Is/As to identify the writer failure","Ensure the output destination has capacity (disk/memory) for bodyLen + checksumSize bytes","Retry the compression of the block","If output is custom, verify its Write implementation handles the small checksum append"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if checksum == nil || len(checksum) != checksumSize { return errors.New(\"checksum must be full size before appending\") }","typeGuard":"func isChecksumWriteError(err error) bool { return err != nil && strings.Contains(err.Error(), \"error writing checksum\") }","tryCatchPattern":"block, err := alg.Compress(body)\nif err != nil {\n    if strings.Contains(err.Error(), \"error writing checksum\") {\n        // output writer failed; retry with fresh buffer/storage\n    }\n    return err\n}","preventionTips":["Use bytes.Buffer for the output accumulator so small appends cannot fail","Ensure storage has capacity for bodyLen+checksumSize before compressing","Unwrap with errors.Is/As to catch disk-full early"],"tags":["io","checksum","filestore"],"backgroundTag":"checksum-write-failed","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}