Skip to content

Conversation

@orsondmc
Copy link
Contributor

@orsondmc orsondmc commented Jul 14, 2025

Summary

May resolve #1741 . In error cases, the httpResponse is not closed and this could lead to a connection leak.

@windsurf-bot
Copy link
Contributor

windsurf-bot bot commented Jul 14, 2025

PR review rate limit exceeded

@CLAassistant
Copy link

CLAassistant commented Jul 14, 2025

CLA assistant check
All committers have signed the CLA.

@orsondmc orsondmc changed the title Ensure proper closure of httpResponse in error scenarios during ret… Ensure proper closure of httpResponse in error scenarios during retries Jul 14, 2025
@theoplegends
Copy link

lgtm!

@mshustov mshustov requested a review from Copilot July 14, 2025 16:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds explicit closing of the HTTP response in the retry loop to prevent potential connection leaks and cleans up some imports and Javadoc links.

  • Ensure ClassicHttpResponse is closed on exception paths during retries
  • Simplify URL instantiation and catch clauses by updating imports
  • Correct Javadoc link reference for setClientCertificate

try {
httpResponse.close();
} catch (IOException ex) {
throw new ClientException("Failed to close response", e);
Copy link

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ClientException is wrapping the original exception e instead of the IOException ex from the close operation. It should pass ex as the cause to accurately represent the close failure.

Suggested change
throw new ClientException("Failed to close response", e);
throw new ClientException("Failed to close response", ex);

Copilot uses AI. Check for mistakes.
try {
httpResponse.close();
} catch (IOException ex) {
throw new ClientException("Failed to close response", e);
Copy link

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing a ClientException inside the close catch block will exit the retry loop prematurely, potentially skipping further retry attempts. Consider logging the close error instead and allowing the retry logic to continue.

Suggested change
throw new ClientException("Failed to close response", e);
LOG.error("Failed to close response", ex);

Copilot uses AI. Check for mistakes.
Endpoint selectedEndpoint = getNextAliveNode();
RuntimeException lastException = null;
for (int i = 0; i <= retries; i++) {
ClassicHttpResponse httpResponse = null;
Copy link

Copilot AI Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using a try-with-resources statement for ClassicHttpResponse to ensure it's always closed automatically, simplifying the error-handling logic.

Copilot uses AI. Check for mistakes.
@mshustov mshustov requested a review from chernser July 14, 2025 20:31
@orsondmc
Copy link
Contributor Author

I have been running this overnight and have not seen any of the leaks I was seeing.

TBH, I feel like the entire way HTTP connections are being handled and closed needs to be rewritten in a safer way. The way it works right now is too complicated - transport is intertwined with the logic of the Client API. Happy to contribute a proposal for that as a follow up to this PR.

@chernser chernser merged commit aa7b910 into ClickHouse:main Jul 17, 2025
15 of 23 checks passed
@chernser
Copy link
Contributor

@orsondmc Thank you for the contribution!

@orsondmc
Copy link
Contributor Author

My pleasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When spawning more than 16 requests with Java 11 HTTP client: com.clickhouse.client.ClickHouseException: Code: 159. Execution timed out

4 participants