ClickHouseServerException: ErrorCode loses the last digit when the number ends the error text

Open Beginner friendly
#796 1 comment 0 reactions 0 assignees View on GitHub

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
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
api, backend

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

api-diagnostics bug
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-Code header. 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
  1. 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.
  2. Compare each ErrorCode with 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 main at a0164a2f
  • .NET version: SDK 11.0.100; the test on main ran 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

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/clickhouse-cs

All issues in ClickHouse/clickhouse-cs

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.