Skip to content

[r2dbc] Fix response leaks on query cancellation - #3354

Open
utkarshb1209 wants to merge 1 commit into
ClickHouse:mainfrom
utkarshb1209:fix/2976-r2dbc-response-cleanup
Open

utkarshb1209 wants to merge 1 commit into
ClickHouse:mainfrom
utkarshb1209:fix/2976-r2dbc-response-cleanup

Conversation

@utkarshb1209

@utkarshb1209 utkarshb1209 commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary

Fixes resource leaks in the R2DBC query execution flow when a subscriber cancels a query or result consumption terminates with an error. Ensures that responses are closed when they are discarded after cancellation and when result consumption completes, fails, or is cancelled.

Closes #2976


Note

Medium Risk
Changes reactive stream cancellation and future bridging behavior; incorrect ownership logic could close responses too early or still leak under races, though new tests target those cases.

Overview
Fixes ClickHouse response leaks when R2DBC subscribers cancel or stop consuming results before the stream finishes.

ClickHouseStatement now routes executes through executeRequest, bridging CompletableFuture completion without propagating Reactor cancel to the underlying future (non-cancellable FutureBridge). Discarded ClickHouseResponse and ClickHouseResult instances are closed via doOnDiscard.

ClickHouseResult keeps the response reference and closes it in doFinally (replacing doOnComplete) through an idempotent close(), so cancel, error, and complete paths all release resources.

Adds ClickHouseResourceCleanupTest covering late completion after cancel, cancel/completion races, successful handoff without premature close, and result map cancel/error cleanup.

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

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.

[R2DBC] Cancelled reactive queries leak HTTP connections and exhaust the connection pool

1 participant