compress: a stream that ends right after a block header reads as io.EOF, the same as a clean end
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 69/100
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
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
- Compress two blocks of 40 bytes with
compress.Writer. - Cut the stream at the start of block 2, 25 bytes after it, and 26 bytes after it.
- Read each cut stream with
compress.NewReaderuntil 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
readBlockreads the block body withio.ReadFull(compress/reader.go:57). When no body byte arrives,io.ReadFullreturnsio.EOF, notio.ErrUnexpectedEOF.Readwraps every error (compress/reader.go:120), andio.ReadFullchanges an EOF after a partial read toio.ErrUnexpectedEOFonly when the error isio.EOFitself. Theio.EOFdocs 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:
mainat 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/ch-go
-
bug connectivity
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1264 ·
Maintainers usually reply within 1 day
-
proto: ColTime32 has the type name "Time32", which the server does not know, so a Time column cannot be written or readPossibly taken @shafiktanbir claimed this 8 days ago. Openbug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
ClickHouse/ch-go#1250 ·
Maintainers usually reply within 1 day
-
proto: Time64(4), (5), (7) and (8) values are cut to milliseconds on write and readPossibly taken @AmanBoronbaev claimed this 8 days ago. Openbug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1248 ·
Maintainers usually reply within 1 day
-
compress: block buffers get re-zeroed on every call, and the zeroing is immediately overwrittenPossibly taken @deepanshu75 claimed this 11 days ago. Openconnectivity enhancement native-protocol performance
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1218 · 1 comment ·
Maintainers usually reply within 1 day
-
ch.Parameters does not escape quotes/backslashes in string valuesPossibly taken @Laotree claimed this 85 days ago. Openbug native-protocol queries
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/ch-go#1198 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/ch-go
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
seriousm4x/UpSnap#1786 ·
-
bug feedback ui/ux
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
peasant-labs/peasant#1307 ·
Maintainers usually reply within 1 day
-
Reformat test names for single-protocol tests to start with Protocol/, i.e. gRPC/ or Websocket/Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
connectrpc/conformance#1211 ·
Maintainers usually reply within 1 day
-
bug effort:s impact:med raised-by:desk risk:low
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
medici-finance/assay#8342 · 1 comment ·
Maintainers usually reply within 1 day
-
hardening
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
inference-sim/inference-sim#2821 ·
Maintainers usually reply within 1 day