Skip to content

[client-v2] An Error thrown while an insert writes its body shuts down the connection pool, so every later request of the Client fails #3453

Backlog

Description

@polyglotAI-bot

Description

When an Error (not an Exception) is thrown while client-v2 writes the body of an insert, the Client stays unusable: each later request fails with IllegalStateException: Connection pool shut down, and ping() returns false. Only a new Client works again.

The program below shows it on three entry points:

  • insert(table, DataStreamWriter, format, settings): the writer throws AssertionError;
  • insert(table, InputStream, format): read() of the stream throws ExceptionInInitializerError;
  • insert(table, List<POJO>): the serialization throws StackOverflowError for a Map nested 10,000 levels deep in a Dynamic column.

A RuntimeException from the same writer fails only that insert. An Error that the caller throws while it reads a query response does not shut down the pool.

Root cause. Apache HttpClient 5 closes its connection manager when an Error passes through MainClientExec.execute (httpclient5 5.6.4, MainClientExec.java:185-187: connectionManager.close(CloseMode.IMMEDIATE)), and client-v2 writes the insert body inside that call: HttpAPIClientHelper.createRequest (HttpAPIClientHelper.java:903) gives the write callback to an EntityTemplate.

The fix is a wrapper of the write callback in HttpAPIClientHelper.createRequest. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #3377 removes one trigger (a Dynamic value that contains itself, #3177), not this effect.

Steps to reproduce

  1. Start a ClickHouse server on localhost:8123.
  2. Run the program of "Code Example" with client-v2 from main (java -cp <client-v2 classpath> Repro.java).
  3. Each case makes one insert that fails, then runs SELECT 1 and ping() on the same Client.

Error Log or Exception StackTrace

writer throws AssertionError: insert threw java.lang.AssertionError
writer throws AssertionError: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
InputStream.read throws ExceptionInInitializerError: insert threw java.lang.ExceptionInInitializerError
InputStream.read throws ExceptionInInitializerError: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
POJO with a Map nested 10000 levels deep: insert threw java.lang.StackOverflowError
POJO with a Map nested 10000 levels deep: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
contrast: writer throws IllegalStateException: insert threw com.clickhouse.client.api.ClickHouseException
contrast: writer throws IllegalStateException: next request OK, ping() = true

The Error passes through MainClientExec:

java.lang.AssertionError: boom
	...
	at com.clickhouse.client.api.Client.lambda$insert$2(Client.java:1878)
	at org.apache.hc.core5.http.io.entity.EntityTemplate.writeTo(EntityTemplate.java:123)
	...
	at org.apache.hc.client5.http.impl.classic.MainClientExec.execute(MainClientExec.java:120)
	...
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.doExecuteRequest(HttpAPIClientHelper.java:837)

The next request:

com.clickhouse.client.api.ClientException: Failed to get query response
	at com.clickhouse.client.api.Client.queryAll(Client.java:2252)
Caused by: com.clickhouse.client.api.ClickHouseException: Query request failed (attempt: 1, duration: 0ms, queryId: null)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.wrapException(HttpAPIClientHelper.java:1250)
Caused by: java.lang.IllegalStateException: Connection pool shut down
	at org.apache.hc.core5.pool.LaxConnPool.lease(LaxConnPool.java:163)
	at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.lease(PoolingHttpClientConnectionManager.java:366)

Expected Behaviour

Only the insert that throws fails, as with a RuntimeException: the caller gets the Error, and the next request on the same Client runs.

Code Example

Reproduction program (Java, 63 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.data.ClickHouseFormat;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class Repro {
    public static class Row {
        private int id; private Object v;
        public Row() {}
        public Row(int id, Object v) { this.id = id; this.v = v; }
        public int getId() { return id; } public void setId(int id) { this.id = id; }
        public Object getV() { return v; } public void setV(Object v) { this.v = v; }
    }

    static Client newClient() {
        return new Client.Builder().addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword("").build();
    }

    static void run(String label, Consumer<Client> failingInsert) {
        try (Client client = newClient()) {
            try {
                failingInsert.accept(client);
            } catch (Throwable t) {
                System.out.println(label + ": insert threw " + t.getClass().getName());
            }
            try {
                client.queryAll("SELECT 1");
                System.out.println(label + ": next request OK, ping() = " + client.ping());
            } catch (Exception e) {
                Throwable root = e; while (root.getCause() != null) root = root.getCause();
                System.out.println(label + ": next request FAILED: " + root + ", ping() = " + client.ping());
            }
        }
    }

    public static void main(String[] args) {
        try (Client client = newClient()) {
            client.queryAll("CREATE OR REPLACE TABLE t_s (id Int32) ENGINE = Memory");
            client.queryAll("CREATE OR REPLACE TABLE t_d (id Int32, v Dynamic) ENGINE = MergeTree ORDER BY id");
        }
        run("writer throws AssertionError", c -> c.insert("t_s",
                out -> { throw new AssertionError("boom"); },
                ClickHouseFormat.RowBinary, new InsertSettings()).join());
        run("InputStream.read throws ExceptionInInitializerError", c -> c.insert("t_s", new InputStream() {
                    @Override public int read() { throw new ExceptionInInitializerError("boom"); }
                }, ClickHouseFormat.RowBinary).join());
        run("POJO with a Map nested 10000 levels deep", c -> {
            c.register(Row.class, c.getTableSchema("t_d"));
            Object v = "leaf";
            for (int i = 0; i < 10_000; i++) { Map<String, Object> m = new HashMap<>(); m.put("k", v); v = m; }
            c.insert("t_d", Collections.singletonList(new Row(1, v))).join();
        });
        run("contrast: writer throws IllegalStateException", c -> c.insert("t_s",
                out -> { throw new IllegalStateException("boom"); },
                ClickHouseFormat.RowBinary, new InsertSettings()).join());
    }
}

Configuration

Client Configuration

new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword("").build()

Environment

  • Cloud
  • Client version: client-v2 from main 8480ca0 (0.12.0-rc1-SNAPSHOT), httpclient5 5.6.4
  • Language version: Java 17.0.21
  • OS: Linux

ClickHouse Server

  • ClickHouse Server version: 26.11.2.4
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the program
  • Sample data for all these tables: none

Classification: impact: Stability · area: Connectivity (connection pool)


Found by automated analysis (Polyglot AI) while working on #3177. Verified against a live server.

Activity

  1. added
    client
    connectivity
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclientconnectivity

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions