nats-io/nats-server · error

failed to compress block: %w

Error message

failed to compress block: %w

What it means

This error is returned when writing a message block to disk in NATS JetStream's file-based message store. During block update, the message buffer is compressed with the algorithm selected for the block (e.g. s2); if alg.Compress(buf) fails, the update is aborted with this wrapped error and the temporary file is cleaned up. It indicates a failure in the compression layer, not in message delivery itself.

Source

Thrown at server/filestore.go:8041

	if err != nil {
		return fmt.Errorf("failed to create temporary file: %w", err)
	}

	errorCleanup := func(err error) error {
		_ = tmpFD.Close()
		_ = os.Remove(tmpFN)
		return err
	}

	alg := NoCompression
	if calg := mb.fs.fcfg.Compression; calg != NoCompression && allowCompress {
		alg = calg
		// The original buffer at this point is uncompressed, so we will now compress
		// it if needed. Note that if the selected algorithm is NoCompression, the
		// Compress function will just return the input buffer unmodified.
		originalSize := len(buf)
		if buf, err = alg.Compress(buf); err != nil {
			return errorCleanup(fmt.Errorf("failed to compress block: %w", err))
		}

		// We only need to write out the metadata header if compression is enabled.
		// If we're trying to uncompress the file on disk at this point, don't bother
		// writing metadata.
		meta := &CompressionInfo{
			Algorithm:    alg,
			OriginalSize: uint64(originalSize),
		}
		buf = append(meta.MarshalMetadata(), buf...)
	}

	// Re-encrypt the block if necessary.
	if err = mb.encryptOrDecryptIfNeeded(buf); err != nil {
		return errorCleanup(err)
	}

	// Write the new block data (which might be compressed or encrypted) to the

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Check available memory; compression allocates output buffers and can fail under memory pressure.
  2. Retry the operation; if the error is transient, the block temp file is cleaned up so the original block remains intact.
  3. If persistent, disable stream compression or move the stream to a host with more resources.
  4. Report/check for compression library (s2/snappy) issues on the NATS version in use; upgrade NATS Server.

Example fix

// before: stream config with compression
Compression: nats.FileCompressS2
// after: temporarily disable compression to isolate the failure
Compression: nats.NoCompression
Defensive patterns

Strategy: retry

Try / catch

// retry with backoff; compression failure aborts update but leaves original block intact
for i := 0; i < 3; i++ {
    if err := updateOp(); err == nil { break }
    time.Sleep(time.Duration(1<<i) * 100 * time.Millisecond)
}

Prevention

When it happens

Trigger: Calling stream/msg-store APIs that rewrite or update an existing message block (e.g. updating a message in a stream with compression enabled) when the compressor's Compress() call returns an error on the block buffer.

Common situations: Compressed stream files (file store with compression configured), corrupted or out-of-memory conditions during buffer allocation inside the compression library, or oversized/malformed block buffers in low-memory environments.

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/fc1c8bb8248824bb. Report an issue: GitHub.