Skip to content

fix: close client after query transport errors - #1203

Open
OrangeFlag wants to merge 14 commits into
ClickHouse:mainfrom
OrangeFlag:fix-pool-transport-error
Open

OrangeFlag wants to merge 14 commits into
ClickHouse:mainfrom
OrangeFlag:fix-pool-transport-error

Conversation

@OrangeFlag

@OrangeFlag OrangeFlag commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Fix query cleanup so chpool cannot reuse a connection with an unread or partially written stream. Transport, protocol and callback failures close the connection. Pending writes are interrupted on cancellation; connections with failed or incomplete sends are closed.

A valid ClickHouse exception leaves the connection reusable after a successful write, including when the sending goroutine has not yet recorded completion. The decoded server exception remains available through IsErr / AsException if interrupting an incomplete send also produces a transport error.

Regression tests cover read/write failures, blocked writes, cancellation with write deadlines, buffered input, callback errors and real-server exception recovery during delayed write completion.

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

Note

Medium Risk
Changes core query/ping/flush error handling and when connections close versus stay reusable; incorrect logic could drop valid pooled connections or leave broken ones open.

Overview
Hardens connection lifecycle after failed or interrupted protocol exchanges so pooled clients are not reused with stale partial reads/writes.

flush now uses context.AfterFunc to force write deadlines on cancellation, aggregate errors with multierr, and close the connection when deadline manipulation fails—without tearing down a conn whose write already completed.

Ping and Do document and enforce: close on transport/protocol/callback failures and incomplete sends; keep the connection open when the query fully sent and the server returned a decodable exception. Do tracks sendComplete and decodedException, closes early when input never ships (e.g. missing column info), and preserves exception errors even when cancellation interrupts pending writes.

Extensive regression tests cover canceled/blocked writes, read timeouts, transport errors, buffered input during exceptions, and reuse after a successful write plus server exception.

Reviewed by Cursor Bugbot for commit 9577258. Bugbot is set up for automated code reviews on this repo. Configure here.

@OrangeFlag

Copy link
Copy Markdown
Contributor Author

@kavirajk please take a look when you have time

@OrangeFlag

Copy link
Copy Markdown
Contributor Author

@chernser, maybe you could take a look?

@OrangeFlag

Copy link
Copy Markdown
Contributor Author

@kavirajk, could you please review this when you have a moment? The race in the regression test is fixed, and CI is green.

@OrangeFlag

Copy link
Copy Markdown
Contributor Author

Hi @kavirajk, could you review this PR when you have a moment?

@OrangeFlag
OrangeFlag force-pushed the fix-pool-transport-error branch from 3652572 to ff7cf51 Compare November 11, 2026 16:41
@security-ai-gate-reviewer

Copy link
Copy Markdown

AI review gate — auto-approval withheld

The AI review gate is green, but the approver did not approve b0094e8d: it comes from a fork, not a branch in this repository: changes from outside need a maintainer's approval.

What to do: ask someone with write access who is not the PR author to approve this pull request.

@OrangeFlag

Copy link
Copy Markdown
Contributor Author

Hi @kavirajk, could you take another look at this PR when you have a moment?

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 220636c. Configure here.

Comment thread client.go Resolved
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

AI review gate — AI review passed

Clean review from: Cursor Bugbot.

@OrangeFlag
OrangeFlag force-pushed the fix-pool-transport-error branch from a0e82c3 to f41da7a Compare November 20, 2026 13:40
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.

1 participant