Back
Open Source

ClickHouse/ch-go: the cancel packet that started with 00

ClickHouse/ch-go #1239 fixed query cancellation by sending only the cancel opcode. make([]byte, 1) left a leading zero byte that the server read as a Hello packet.

JB
Josué Barros
••2 min read

Client.cancelQuery allocated make([]byte, 1) before encoding ClientCodeCancel. ClientCode.Encode appends with PutByte, so the buffer already contained a zero byte. The client sent 00 03 instead of 03.

This is one of the pull requests behind Six merged PRs in 72 hours. The series looks at the failure, the fix and the review evidence, one merge at a time.

The change: +62 / -1 across 2 files. PR 1239.

The bug

The server read the leading 00 as a Hello packet and recorded the cancellation as a protocol error instead of QUERY_WAS_CANCELLED_BY_CLIENT. The query was cancelled, but the protocol conversation was wrong.

The bug is a one-byte difference in a wire format. It is also a reminder that “allocate a buffer” and “allocate a buffer with length zero” are different operations.

Why it mattered

Query cancellation is a safety valve. A long-running query can hold connections, memory and concurrency slots. If cancellation is reported as a protocol error, the client and server disagree about what happened, and cleanup can be delayed or misdiagnosed.

The fix

Allocate an empty buffer with capacity for the single cancel byte: make([]byte, 0, 1). No API change. The connection is still closed after cancellation.

The evidence

TestClientCancelQueryPacket dials a real ClickHouse server through a recording Dialer, cancels SELECT sleep(2), and asserts the last write is exactly []byte{03}. The test fails before the fix with 00 03 and passes after. The PR also runs go test ./..., focused race tests, go vet, gofmt and manual checks on ClickHouse 25.8, 26.9 and 26.10.

What I would repeat

For binary protocols, assert the exact bytes on the wire. A higher-level test that only checks “cancelled” can miss a malformed packet that the server happens to tolerate or report differently. Record the write and compare it.

Sources

If this was useful, connect with me on LinkedIn; I write about the bugs behind merged pull requests and the evidence that gets them accepted.

FAQ

What was the ClickHouse/ch-go cancellation bug?
The cancel packet was sent as 00 03 instead of 03 because the buffer was allocated with length one, so the server read a leading Hello byte.
What is the fix?
Use make([]byte, 0, 1) so the cancel opcode is the only payload.
How was it tested?
A recording Dialer captures the last write and asserts it equals []byte{03}; the test fails on the old code.
#clickhouse#go#protocol#tcp#open-source

Join the Newsletter

Subscribe to get my latest content by email.

We won't send you spam. Unsubscribe at any time.