From d5455db1104f2ec9f00a3ff465a6f6c4aa740e40 Mon Sep 17 00:00:00 2001 From: Aleksei Griaznov Date: Fri, 17 Oct 2025 14:01:30 +0400 Subject: [PATCH 1/3] Refactor HttpAPIClientHelper to improve response cleanup handling --- .../main/java/com/clickhouse/client/api/Client.java | 3 ++- .../client/api/internal/HttpAPIClientHelper.java | 12 +++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index ec4c98ff3..b17562b74 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -1605,6 +1605,7 @@ public CompletableFuture query(String sqlQuery, Map query(String sqlQuery, Map r if (httpResponse.getCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { throw new ClientMisconfigurationException("Proxy authentication required. Please check your proxy settings."); } else if (httpResponse.getCode() == HttpStatus.SC_BAD_GATEWAY) { - httpResponse.close(); throw new ClientException("Server returned '502 Bad gateway'. Check network and proxy settings."); } else if (httpResponse.getCode() >= HttpStatus.SC_BAD_REQUEST || httpResponse.containsHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE)) { - try { - throw readError(httpResponse); - } finally { - httpResponse.close(); - } + throw readError(httpResponse); } return httpResponse; @@ -473,10 +468,13 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map r closeQuietly(httpResponse); LOG.warn("Failed to connect to '{}': {}", server.getBaseURL(), e.getMessage()); throw e; + } catch (Exception e) { + closeQuietly(httpResponse); + throw e; } } - public void closeQuietly(ClassicHttpResponse httpResponse) { + public static void closeQuietly(ClassicHttpResponse httpResponse) { if (httpResponse != null) { try { httpResponse.close(); From 617dcbba8c97e5781618f2068acd470a5c060a81 Mon Sep 17 00:00:00 2001 From: Aleksei Griaznov Date: Tue, 21 Oct 2025 11:19:22 +0400 Subject: [PATCH 2/3] Refactor HttpAPIClientHelper to improve response cleanup handling. Bring back in-place response cleanup --- .../client/api/internal/HttpAPIClientHelper.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java index 5d13d8591..1aa67b17c 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java @@ -454,9 +454,14 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map r if (httpResponse.getCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { throw new ClientMisconfigurationException("Proxy authentication required. Please check your proxy settings."); } else if (httpResponse.getCode() == HttpStatus.SC_BAD_GATEWAY) { + httpResponse.close(); throw new ClientException("Server returned '502 Bad gateway'. Check network and proxy settings."); } else if (httpResponse.getCode() >= HttpStatus.SC_BAD_REQUEST || httpResponse.containsHeader(ClickHouseHttpProto.HEADER_EXCEPTION_CODE)) { - throw readError(httpResponse); + try { + throw readError(httpResponse); + } finally { + httpResponse.close(); + } } return httpResponse; From 593ab6d4b6179e50acbcca5d3e2efc07e42fcee6 Mon Sep 17 00:00:00 2001 From: Aleksei Griaznov Date: Tue, 21 Oct 2025 11:39:34 +0400 Subject: [PATCH 3/3] Refactor HttpAPIClientHelper to improve response cleanup handling. Logging for unexpected exceptions --- .../com/clickhouse/client/api/internal/HttpAPIClientHelper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java index 1aa67b17c..df449c90e 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java @@ -475,6 +475,7 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map r throw e; } catch (Exception e) { closeQuietly(httpResponse); + LOG.debug("Failed to execute request to '{}': {}", server.getBaseURL(), e.getMessage(), e); throw e; } }