Fix clickhouse-data: make NonBlockingPipedOutputStream.close() idempotent - #3091
Open
jjj-n wants to merge 1 commit into
Open
Fix clickhouse-data: make NonBlockingPipedOutputStream.close() idempotent#3091jjj-n wants to merge 1 commit into
jjj-n wants to merge 1 commit into
Conversation
Claim the close path atomically so only one concurrent caller flushes the pending buffer, enqueues EOF, and runs the post-close action. Fixes: ClickHouse#3057
chernser
approved these changes
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make
NonBlockingPipedOutputStream.close()atomically claim the close path before flushing its pending buffer. Previously, two concurrent callers could both pass thevolatile closedcheck, mutate and enqueue the sameByteBuffer, enqueue the end-of-stream marker twice, and run the post-close action twice. If the second caller changed the already-queued buffer's limit to zero, the reader observed a clean EOF and silently lost the pending payload.Exactly one caller now flushes pending data, enqueues the end-of-stream marker, and runs the post-close action. Concurrent or repeated
close()calls return immediately. The implementation follows theAtomicBooleanpattern already used byBlockingPipedOutputStream, without holding the instance monitor across the close path or a potentially blocking post-close action.Closes #3057
Compatibility
There is no public API, configuration, default, output-format, serialization, or exception-type change. A single
close()keeps its existing behavior. Only a redundant concurrent close changes from racing with the first caller to returning without touching the buffer or queue.docs/features.mdis not affected because this change is confined toclickhouse-data.Test
Added
testConcurrentClose, which holds the first close at the pending-buffer offer, invokes a second close concurrently, and verifies that the second caller returns without waiting, the post-close action runs exactly once, the pending bytes are preserved, and the reader sees EOF afterward.expected [1] but found [2]for the post-close action count.mvn -pl clickhouse-data -Dtest=NonBlockingPipedOutputStreamTest test: 6 tests passed.mvn -pl clickhouse-data test: 1,668 tests passed; 113 skipped by the existing suite configuration.mvn -Dj8 -pl clickhouse-data -Dtest=NonBlockingPipedOutputStreamTest test: Java 8 source/target compilation succeeded and 6 tests passed.Checklist
CHANGELOG.md