From 480e75447155fa88b99b6306ba93d3083ba88fd3 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Wed, 10 Jun 2026 16:31:10 -0400 Subject: [PATCH] client: clear Inserting state before connect in ResetConnection ResetConnection() connects first, then clears the inserting state. When the reconnect throws, the state stays Inserting. The destructor then calls EndInsert() on the old dirty socket. Move the clear to the first line. --- clickhouse/client.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clickhouse/client.cpp b/clickhouse/client.cpp index b5d287bc..1f93c39b 100644 --- a/clickhouse/client.cpp +++ b/clickhouse/client.cpp @@ -606,6 +606,13 @@ void Client::Impl::Ping() { } void Client::Impl::ResetConnection() { + // Clear the insert state BEFORE attempting to reconnect. If connect() + // throws (server at max_connections, ephemeral-port exhaustion, DNS blip, + // etc. — all cases where the original dirty socket may still be healthy), + // a still-Inserting state would make ~Impl's automatic EndInsert() fire + // on the old dirty wire and commit a partially-streamed insert. + state_ = State::Idle; + InitializeStreams(socket_factory_->connect(options_, current_endpoint_.value())); state_ = State::Idle;