ClickHouseServerException: ErrorCode loses the last digit when the number ends the error text
Maintainers usually reply within 1 day
@thekaranatic is already working on this.
Since Dec 3, 2026.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 82/100
Research direction
Read ClickHouse.Driver/ClickHouseServerException.cs, especially ParseErrorCode around line 39 and lines 55–64. Check how the end index handles a number at the end of the error text, then run the reproduction program from the issue or add focused coverage for FromServerResponse and the HTTP error path. Done means ErrorCode preserves the full final digit, including for a one-digit code.
Written by the indexing model from the issue text.
Description
Describe the bug
ClickHouseServerException.ErrorCode loses the last digit of the number when the first number of the error text is also the end of the text. When that number has only one digit, ErrorCode is -1. The exception message is correct.
Affected entry points:
- The HTTP error path, when a response has a non-success status, a body, and no
X-ClickHouse-Exception-Codeheader. For example, the plain-text error body of a proxy or load balancer in front of ClickHouse. - The public method
ClickHouseServerException.FromServerResponse(error, query).
The wrong values can be valid ClickHouse codes with a different meaning: 101 is UNEXPECTED_PACKET_FROM_CLIENT, 6 is CANNOT_PARSE_TEXT. A ClickHouse server error body (Code: 60. DB::Exception: ...) is not affected: a . follows the code. The same text with any character after the number (for example \n) also gives the correct code.
Steps to reproduce
- Run the code example (
dotnet run app.cs, .NET 10 or later). It needs no ClickHouse server: a local socket sends one proxy-style response. - Compare each
ErrorCodewith the number in the text.
Expected behaviour
ErrorCode is the complete first number of the text: 1015 for error code: 1015, 60 for Code: 60, 5 for 5. This is the result for the same text followed by \n (second line of the log).
Code example
Reproduction program (C#, 32 lines)
#:package [email protected]
using System.Net;
using System.Net.Sockets;
using System.Text;
using ClickHouse.Driver;
// A stand-in for a proxy in front of ClickHouse: one plain-text error response with no X-ClickHouse-Exception-Code header.
async Task<int> CodeFromProxy(int status, string body)
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
var serve = Task.Run(async () =>
{
using var tcp = await listener.AcceptTcpClientAsync();
using var stream = tcp.GetStream();
await stream.ReadAsync(new byte[65536]);
var payload = Encoding.UTF8.GetBytes(body);
await stream.WriteAsync(Encoding.ASCII.GetBytes(
$"HTTP/1.1 {status} Proxy\r\nContent-Type: text/plain\r\nContent-Length: {payload.Length}\r\nConnection: close\r\n\r\n"));
await stream.WriteAsync(payload);
});
using var client = new ClickHouseClient($"Host=127.0.0.1;Port={port}");
try { await client.ExecuteNonQueryAsync("SELECT 1"); return 0; }
catch (ClickHouseServerException ex) { await serve; listener.Stop(); return ex.ErrorCode; }
}
foreach (var body in new[] { "error code: 1015", "error code: 1015\n" })
Console.WriteLine($"HTTP 502, body \"{body.Replace("\n", "\\n")}\" -> ErrorCode {await CodeFromProxy(502, body)}");
foreach (var text in new[] { "Code: 60", "5" })
Console.WriteLine($"FromServerResponse(\"{text}\", null) -> ErrorCode {ClickHouseServerException.FromServerResponse(text, null).ErrorCode}");
Error log
HTTP 502, body "error code: 1015" -> ErrorCode 101
HTTP 502, body "error code: 1015\n" -> ErrorCode 1015
FromServerResponse("Code: 60", null) -> ErrorCode 6
FromServerResponse("5", null) -> ErrorCode -1
Root cause: ParseErrorCode starts end at error.Length - 1 (ClickHouse.Driver/ClickHouseServerException.cs:39), and the loop at lines 55–62 changes end only when it finds a non-digit. So a number that ends the text is read without its last character (line 64).
The fix is the start value of end. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: #794 (open) takes the code from the X-ClickHouse-Exception-Code header when the server sends it. A body with no header still goes through ParseErrorCode.
Configuration
Environment
- Client version: 1.5.0 (NuGet), and
mainat a0164a2f - .NET version: SDK 11.0.100; the test on
mainran on net10.0 (runtime 10.0.15) - OS: Ubuntu 26.04 (x86_64, container)
ClickHouse server
- ClickHouse Server version: 26.11.7.10, only for the check that a server error body is not affected
Classification: impact: none · area: API & diagnostics (ClickHouseServerException.ErrorCode)
Found by automated analysis (Polyglot AI) while working on #794. Verified against a live server.
- Dominant language
- C#
- Stars
- 102
- Forks
- 31
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 40
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the 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/clickhouse-cs
-
bug documentation queries
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#815 ·
Maintainers usually reply within 1 day
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-cs#813 ·
Maintainers usually reply within 1 day
-
bug data-types documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#811 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#809 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#806 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-cs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xmlPossibly taken @alexeyfala claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
danipen/TextMateSharp#145 · 2 comments ·
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Klyroo/klyroo-dashboard#12 ·
Maintainers usually reply within 1 day
-
CONTRIBUTING rules Node out in step 0, then tells client-side contributors to run node --test, which is what CI gates onPossibly taken @yadavraj2001 claimed this today. Opendocumentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
juandresrodca/AdminForge#73 · 1 comment ·
Maintainers usually reply within 1 day
-
[辞書] VIVANTOpen要トリアージ 辞書
Difficulty 1/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day