nats-io/nats-server · error
compression algorithm not known
Error message
compression algorithm not known
What it means
StoreCompression only supports NoCompression and S2Compression. Compress() hits its default case when the algorithm value is neither — meaning an unrecognized StoreCompression enum value was used, usually from an invalid byte in stored metadata or a binary built without a newer algorithm. From server/filestore.go.
Source
Thrown at server/filestore.go:14512
return 0, fmt.Errorf("metadata incomplete")
}
return 4 + n, nil
}
func (alg StoreCompression) Compress(buf []byte) ([]byte, error) {
if len(buf) < checksumSize {
return nil, fmt.Errorf("uncompressed buffer is too short")
}
bodyLen := int64(len(buf) - checksumSize)
var output bytes.Buffer
var writer io.WriteCloser
switch alg {
case NoCompression:
return buf, nil
case S2Compression:
writer = s2.NewWriter(&output)
default:
return nil, fmt.Errorf("compression algorithm not known")
}
input := bytes.NewReader(buf[:bodyLen])
checksum := buf[bodyLen:]
// Compress the block content, but don't compress the checksum.
// We will preserve it at the end of the block as-is.
if n, err := io.CopyN(writer, input, bodyLen); err != nil {
return nil, fmt.Errorf("error writing to compression writer: %w", err)
} else if n != bodyLen {
return nil, fmt.Errorf("short write on body (%d != %d)", n, bodyLen)
}
if err := writer.Close(); err != nil {
return nil, fmt.Errorf("error closing compression writer: %w", err)
}
// Now add the checksum back onto the end of the block.
if n, err := output.Write(checksum); err != nil {View on GitHub (pinned to 3a66a489d2)
Solutions
- Confirm the algorithm byte value and that it matches a supported enum (NoCompression, S2Compression)
- Upgrade nats-server to a build that knows the stored compression algorithm
- Fix the corrupted/invalid algorithm byte by re-creating the affected block or consumer
- Validate enum values in code that builds StoreCompression from config or bytes
Example fix
// before
alg := StoreCompression(raw[3]) // 7 — unknown
alg.Compress(block) // compression algorithm not known
// after
switch StoreCompression(raw[3]) {
case NoCompression, S2Compression:
alg.Compress(block)
default:
// rebuild or upgrade server
} Defensive patterns
Strategy: type-guard
Validate before calling
func knownAlgorithm(a StoreCompression) bool {
switch a {
case NoCompression, S2Compression:
return true
}
return false
}
if !knownAlgorithm(alg) { return fmt.Errorf("unsupported compression %d", alg) } Type guard
func asKnownAlg(v byte) (StoreCompression, bool) {
a := StoreCompression(v)
return a, a == NoCompression || a == S2Compression
} Try / catch
out, err := alg.Compress(block)
if err != nil && strings.Contains(err.Error(), "algorithm not known") {
// upgrade server or rebuild block with supported algorithm
return errUnsupportedAlg
} Prevention
- Don't build StoreCompression from raw ints without validation
- Match server version to whatever wrote the store directory
- Whitelist compression algorithms in config parsing
- Log the raw algorithm byte when it fails
When it happens
Trigger: Decoding a block whose algorithm byte is outside the known enum, or code passing an uninitialized/invalid StoreCompression value into Compress().
Common situations: Store directories written by a newer nats-server with an algorithm this build doesn't know; corrupted metadata byte 3 of the compression header; application code constructing StoreCompression from a raw int.
Related errors
- failed to read original block from disk: %w
- failed to read existing metadata header: %w
- failed to decompress original block: %w
- failed to compress block: %w
- unsupported version: %d
AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02).
Data as JSON: /api/errors/8cb807206ff35b00.
Report an issue: GitHub.