Repository navigation
Description
Description
The ServerException message of client-v2 loses part or all of the body of an error response (400, 401, 403, 404, 500) that has no X-ClickHouse-Exception-Code header. Examples: a proxy or load-balancer error page, or the ClickHouse server's own There is no handle /... 404. There are three defects:
- A body sent with
Transfer-Encoding: chunkedin more than one chunk is cut at the end of the first chunk, also when all bytes arrive in one TCP write. This occurs withcompress=trueand withcompress=false. - With
compress=true(the default), the last 25 bytes of the read are lost. A body of exactly 25 bytes givesunknown server error. - With
compress=true, a body shorter than 25 bytes is lost. The message isunknown server error, and the client logs a WARN with a stack trace.
Steps to reproduce
- Point client-v2 at an HTTP endpoint that returns 404 for
POST /with a plain-text body and noX-ClickHouse-Exception-Codeheader. A small socket server or WireMock is sufficient (see the code example). For defect 2, a real ClickHouse server with a wrong endpoint path (http://host:8123/nope) also works. - Call
client.query("SELECT 1").get(). - Compare
ServerException.getMessage()with the body that was sent.
Error Log or Exception StackTrace
Results on main:
| Body sent | compress=true (default) |
compress=false |
|---|---|---|
404 page not found (18 B) |
unknown server error (transport error: 404) + WARN below |
full body |
| 25 B | unknown server error |
full body |
| 26 B | a (1 char) |
full body |
| 60 B | first 35 chars | full body |
| nginx 404 HTML page (144 B) | ends at <hr><center>nginx</ |
full body |
| 129 B in 3 chunks, one TCP write | upstream connect e (18 chars) |
first chunk only (43 chars) |
ClickHouse server 404 There is no handle /nope |
last 25 bytes lost: the last line is clickhouse instead of clickhouse-client < query > result |
full body |
On the PR #3322 head, rows 2-5 and 7 are correct. Rows 1 and 6 do not change.
WARN for row 1:
WARN HttpAPIClientHelper - Failed to read error message (queryId = )
java.io.IOException: Incomplete read: 18 of 25
at com.clickhouse.client.api.internal.CompressedBlockInputStream.readFully(CompressedBlockInputStream.java:89)
at com.clickhouse.client.api.internal.CompressedBlockInputStream.refill(CompressedBlockInputStream.java:108)
at com.clickhouse.client.api.internal.CompressedBlockInputStream.read(CompressedBlockInputStream.java:63)
at com.clickhouse.client.api.internal.HttpAPIClientHelper.readNotClickHouseError(HttpAPIClientHelper.java:459)
at com.clickhouse.client.api.internal.HttpAPIClientHelper.readError(HttpAPIClientHelper.java:439)
at com.clickhouse.client.api.internal.HttpAPIClientHelper.doExecuteRequest(HttpAPIClientHelper.java:868)
Expected Behaviour
The ServerException message contains the full body text, up to ERROR_BODY_BUFFER_SIZE (1024 bytes). This is what compress=false gives today for a body that arrives in one chunk.
Root cause
readNotClickHouseError(HttpAPIClientHelper.java:459) callsbody.read(buffer, offset, buffer.length - offset)one time, with no loop until EOF or a full buffer. HttpCore 5ChunkedInputStreamreturns at most the rest of the current chunk (defect 1).HttpAPIClientHelper.java:461:new String(buffer, 0, msgLen, ...)ignoresoffset, which is 25 after the "Invalid LZ4 magic byte" fallback (line 473) (defect 2).CompressedBlockInputStream.java:89:readFullythrowsIOException, notClientException, when the body ends before 25 bytes. The fallback atHttpAPIClientHelper.java:467catches onlyClientException, so lines 478-480 discard the bytes already read (defect 3).
For defect 3, a maintainer decides if an error response without X-ClickHouse-Exception-Code goes through CompressedBlockEntity at all.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: open PR #3322 fixes only defect 2, in the same method. Its test testTruncatedCompressedErrorBodyStaysUnreadable covers only a response with the exception-code header.
Code Example
Chunked case (row 6). The server writes the whole response in one write(), so the result does not depend on timing.
Reproduction program (Java, 29 lines)
String[] chunks = {
"upstream connect error or disconnect/reset ",
"before headers. reset reason: connection ",
"failure, transport failure reason: marker-END"};
StringBuilder rsp = new StringBuilder("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n"
+ "Transfer-Encoding: chunked\r\nConnection: close\r\n\r\n");
for (String c : chunks) {
rsp.append(Integer.toHexString(c.length())).append("\r\n").append(c).append("\r\n");
}
rsp.append("0\r\n\r\n");
ServerSocket ss = new ServerSocket(0);
new Thread(() -> {
try (Socket s = ss.accept()) {
// read the request headers up to "\r\n\r\n" (omitted), then:
s.getOutputStream().write(rsp.toString().getBytes(StandardCharsets.UTF_8));
s.shutdownOutput();
// drain the request body (omitted)
} catch (Exception ignore) { }
}).start();
try (Client client = new Client.Builder()
.addEndpoint(Protocol.HTTP, "localhost", ss.getLocalPort(), false)
.setUsername("default").setPassword("x")
.build()) { // add .compressServerResponse(false) for the contrast column
client.query("SELECT 1").get(10, TimeUnit.SECONDS);
} catch (ServerException e) {
System.out.println(e.getMessage()); // "upstream connect e (transport error: 404)"
}Rows 1-5 use the same client with a WireMock stub: aResponse().withStatus(404).withBody(<body>), no X-ClickHouse-Exception-Code header.
Configuration
Client Configuration
new Client.Builder()
.addEndpoint(Protocol.HTTP, "localhost", port, false)
.setUsername("default").setPassword("x")
.build(); // defaults: compress=true, use_http_compression=falseEnvironment
- Cloud
- Client version:
mainf9586ab (0.12.0-rc1-SNAPSHOT); also checked on PR Improve compression handling #3322 head a7f1322 - Language version: Java 17 (Eclipse Temurin), httpcore5 5.4.3
- OS: Linux (Docker)
ClickHouse Server
- ClickHouse Server version: 26.9.1.1629 (only the real-server row; the other rows use a mock HTTP server)
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved: none- Sample data for all these tables: none
Found by automated analysis (Polyglot AI) while working on #2287 (PR #3335). Verified against a live server. Shortened on 2026-11-20: the edit history has the first version.
Activity