compress: a stream that ends right after a block header reads as io.EOF, the same as a clean end

Open
#1,256 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@AmanBoronbaev is already working on this.

Since Dec 2, 2026.

  • #1263 by @AmanBoronbaev — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
69/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend, databases

Research direction

Start with compress/reader.go, especially readBlock around line 57 and Read around line 120, then run the reproduction program in the issue for both LZ4 and ZSTD. Check how truncated headers and bodies are reported compared with a clean block boundary. Done means incomplete blocks return an error that does not match io.EOF, while a clean boundary still returns io.EOF.

Written by the indexing model from the issue text.

Description

bug connectivity native-protocol
Describe the bug

When a compressed stream ends right after the 25-byte header of a block, compress.Reader.Read returns an error that matches io.EOF (read next block: read raw: EOF). A stream that ends at a block boundary gives the same io.EOF, so the caller cannot tell a truncated stream from the end of the data. A stream that ends inside a block body gives io.ErrUnexpectedEOF, as it should. LZ4 and ZSTD both show it.

Also, when io.ReadFull needs bytes of the next block and the stream ends at the block boundary, it returns the partial read with the io.EOF error, not io.ErrUnexpectedEOF.

A caller that stops at io.EOF gets a partial result with no error: clickhouse-go shows this over HTTP with compression (ClickHouse/clickhouse-go#2176). ch.Client.Do is not affected, because it returns every read error that comes before the EndOfStream packet (query.go:759-777). The same defect was reported for clickhouse-java in https://github.com/ClickHouse/clickhouse-java/issues/3448.

Steps to reproduce
  1. Compress two blocks of 40 bytes with compress.Writer.
  2. Cut the stream at the start of block 2, 25 bytes after it, and 26 bytes after it.
  3. Read each cut stream with compress.NewReader until the first error.
Expected behaviour

A stream that ends before a block is complete gives io.ErrUnexpectedEOF, or another error that does not match io.EOF. The Go io.EOF docs: "If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either ErrUnexpectedEOF or some other error giving more detail."

The server also rejects a stream that ends right after a block header. An HTTP INSERT with decompress=1 of seq 0 49 in LZ4 blocks of 40 bytes (clickhouse compressor -b 40), with block 2 at offset 67:

Cut Server result
at block 2 (67 bytes) HTTP 200, 17 rows inserted
right after block 2's header (92 bytes) Code: 33. DB::Exception: Cannot read all data. Bytes read: 25. Bytes expected: 67. (CANNOT_READ_ALL_DATA)
inside block 2's body (93 bytes) the same error, with Bytes read: 26
Code example
Reproduction program (Go, 55 lines)
package main

import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"io"

	"github.com/ClickHouse/ch-go/compress"
)

func main() {
	for _, m := range []compress.Method{compress.LZ4, compress.ZSTD} {
		// Two compressed blocks of 40 bytes each.
		w := compress.NewWriter(compress.LevelZero, m)
		var stream []byte
		for _, c := range []byte("ab") {
			if err := w.Compress(bytes.Repeat([]byte{c}, 40)); err != nil {
				panic(err)
			}
			stream = append(stream, w.Data...)
		}
		// Block 2 starts after the checksum (16 bytes) and the compressed size of block 1.
		block2 := 16 + int(binary.LittleEndian.Uint32(stream[17:]))
		for _, c := range []struct {
			name string
			cut  int
		}{
			{"at the block boundary", block2},
			{"right after block 2's header", block2 + 25},
			{"inside block 2's body", block2 + 26},
		} {
			got, err := readAll(compress.NewReader(bytes.NewReader(stream[:c.cut])))
			fmt.Printf("%-4s %-29s %2d bytes, io.EOF: %-5v err: %v\n",
				m, c.name+":", got, errors.Is(err, io.EOF), err)
		}
		// A read of 60 bytes when the stream ends at the block boundary (block 1 has 40).
		n, err := io.ReadFull(compress.NewReader(bytes.NewReader(stream[:block2])), make([]byte, 60))
		fmt.Printf("%-4s io.ReadFull of 60 bytes, cut at the block boundary: n=%d, io.EOF: %v, err: %v\n",
			m, n, errors.Is(err, io.EOF), err)
	}
}

// readAll reads until the first error, as a caller that stops at io.EOF does.
func readAll(r io.Reader) (int, error) {
	buf, total := make([]byte, 1024), 0
	for {
		n, err := r.Read(buf)
		total += n
		if err != nil {
			return total, err
		}
	}
}
Error log
LZ4  at the block boundary:        40 bytes, io.EOF: true  err: read next block: header: EOF
LZ4  right after block 2's header: 40 bytes, io.EOF: true  err: read next block: read raw: EOF
LZ4  inside block 2's body:        40 bytes, io.EOF: false err: read next block: read raw: unexpected EOF
LZ4  io.ReadFull of 60 bytes, cut at the block boundary: n=40, io.EOF: true, err: read next block: header: EOF
ZSTD at the block boundary:        40 bytes, io.EOF: true  err: read next block: header: EOF
ZSTD right after block 2's header: 40 bytes, io.EOF: true  err: read next block: read raw: EOF
ZSTD inside block 2's body:        40 bytes, io.EOF: false err: read next block: read raw: unexpected EOF
ZSTD io.ReadFull of 60 bytes, cut at the block boundary: n=40, io.EOF: true, err: read next block: header: EOF
Root cause
  • readBlock reads the block body with io.ReadFull (compress/reader.go:57). When no body byte arrives, io.ReadFull returns io.EOF, not io.ErrUnexpectedEOF.
  • Read wraps every error (compress/reader.go:120), and io.ReadFull changes an EOF after a partial read to io.ErrUnexpectedEOF only when the error is io.EOF itself. The io.EOF docs say that Read must return EOF itself, not an error that wraps EOF.

The fix is in readBlock and Read; clickhouse-go then needs a ch-go update for #2176. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #1244 (open) changes the buffers of readBlock, not its EOF handling.

Configuration
Environment
  • Client version: main at d9ee203 (after v0.76.0)
  • Language version: Go 1.26.11
  • OS: Linux amd64
ClickHouse server
  • ClickHouse Server version: 26.11.2.4 (only for the server check above; the program needs no server)

Classification: impact: Data integrity · area: Connectivity (compressed blocks) · native: protocol


Found by automated analysis (Polyglot AI) while checking clickhouse-java#3448 against the other client repos. Verified against a live server.

Dominant language
Go
Stars
435
Forks
101
Avg merge
22h 1m
Merged PRs (30d)
15

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/ch-go

All issues in ClickHouse/ch-go

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.