Repository navigation
Conversation
…ing collector The interruptible client output feature (ClickHouse#22426, ClickHouse#106430) installs a cancellation hook on the persistent std_out WriteBufferFromFileDescriptor at the start of processOrdinaryQuery and removed it via a scope guard at the end of the same scope. When the result set is written with parallel formatting, ParallelFormattingOutputFormat runs a background collector thread that writes through std_out (and reads std_out's cancellation hook in WriteBufferFromFileDescriptor::nextImpl). On the normal end-of-stream path the collector is joined by output_format->finalize() before the scope guard runs, so the clear is safe. But on paths that leave processOrdinaryQuery by exception without first finalizing the output (for example a NetException or a LocalFormatError rethrown from receiveResult), the scope guard cleared the hook while the collector thread was still alive and reading it, a data race on the std::function. ThreadSanitizer reported this from BuzzHouse (amd_tsan) on master (STID 5296-4b74). Clear the hook in resetOutput() right after output_format.reset(), which destroys the parallel formatter and joins its collector thread, so the hook is only mutated once no other thread can read it. resetOutput() runs on every exit path of processParsedSingleQuery (including the SCOPE_EXIT_SAFE that already parks the formatting threads) and at the start of the next query, so the hook is still always removed and std_out (which outlives a single query) never carries a stale hook into the next query. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-0:20260618-040800 |
|
cc @alexey-milovidov @azat — could you review? This fixes a TSan data race that BuzzHouse (amd_tsan) found on master in the interruptible-output feature (#106430): the per-query |
|
Workflow [PR], commit [a232e17] Summary: ✅
AI ReviewSummaryThis PR moves the persistent Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Addressing review feedback on the previous placement: clearing std_out's cancellation hook immediately after output_format.reset() removes the race (the parallel-formatting collector thread is joined by then) but also disables interruptibility for the remaining resetOutput() flushes - out_file_buf and std_out_wrapper finalization and the final std_out->next(). If one of those buffers bytes to a slow or stuck stdout, Ctrl+C could no longer discard the buffer, regressing the blocked-output behavior from ClickHouse#106430. Register the clear as a SCOPE_EXIT right after output_format.reset() instead: it still fires strictly after the collector is joined (so no race), but only at the end of resetOutput(), after the final flushes, so the hook stays active through them and Ctrl+C keeps working. Using a scope guard also keeps the clear on every exit path of resetOutput() (e.g. the SIGPIPE/SIGQUIT restore can throw), matching the original guard's no-leak guarantee. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Reworks the std_out cancellation-hook lifetime to satisfy both the slow-stdout interruptibility of the blocked-output feature (ClickHouse#106430) and the requirement that already-produced output is not silently dropped on exception paths. query_interrupt_handler.cancelled() is true both after a genuine Ctrl+C and after the normal end-of-query stop() (both leave exit_after_signals <= 0), so it cannot by itself distinguish "user cancelled" from "query finished". The earlier revision kept that handler-based hook installed through the resetOutput() teardown flushes; on exception paths processOrdinaryQuery's scope guard has already run stop() before resetOutput() runs (via processParsedSingleQuery's scope guard), making cancelled() unconditionally true and causing the final std_out_wrapper / out_file_buf finalization and std_out->next() to discard output that was already produced. Snapshot whether the interrupt handler is still armed at resetOutput() entry and choose the cancellation source accordingly: armed (normal completion, reached via onEndOfStream while the handler is still active) keeps honoring the handler so a fresh Ctrl+C still discards a flush to a slow/stuck stdout; stopped (exception paths) falls back to the genuine cancellation flag, which is set only by cancelQuery(), so completion is never mistaken for cancellation. The hook is still only mutated after output_format.reset() has joined the parallel-formatting collector that also reads it, so the original data race (TSan, BuzzHouse amd_tsan) remains fixed. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 13/13 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
Closing as obsolete. This PR fixed the TSan data race (STID 5296-4b74) on That feature was reverted on master by #107845 (merged 2026-06-19). The revert removed the entire If the feature is re-landed, the race fix from this PR (clearing the hook only after |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a data race in the client/local on the result-output buffer's cancellation hook (introduced by the interruptible Ctrl+C output feature) when the result set is written with parallel formatting and a query ends by an exception.
Description
A ThreadSanitizer data race was reported on master by
BuzzHouse (amd_tsan):Both racing stacks reference the same long-lived heap object, the client's persistent stdout
AutoCanceledWriteBuffer<WriteBufferFromFileDescriptor>(std_out, allocated once inClientBase::ClientBase):WriteBufferFromFileDescriptor::setCancellationHookresettingcancellation_hook, from the scope guard at the end ofClientBase::processOrdinaryQuery.WriteBufferFromFileDescriptor::nextImplreadingcancellation_hook, viaFlushCallbackWriteBuffer::nextImplfromParallelFormattingOutputFormat::collectorThreadFunction.Root cause: the interruptible client output feature (PR #106430, issue #22426) installs a cancellation hook on
std_outat the start ofprocessOrdinaryQueryand removed it viaSCOPE_EXIT({ std_out->setCancellationHook({}); })at the end of the same scope. When the result set is written with parallel formatting,ParallelFormattingOutputFormatruns a background collector thread that writes throughstd_outand reads its cancellation hook. On the normal end-of-stream path the collector is joined byoutput_format->finalize()before the scope guard runs, so the clear is safe. But on paths that leaveprocessOrdinaryQueryby an exception without first finalizing the output (e.g. aNetException, or aLocalFormatErrorrethrown fromreceiveResult), the scope guard cleared the hook while the collector thread was still alive and reading it: a data race on thestd::function.Fix: clear the hook in
resetOutput()right afteroutput_format.reset().output_format.reset()destroys the parallel formatter and joins its collector thread (~ParallelFormattingOutputFormat->finishAndWait->collector_thread.join()), so afterwards no other thread can readstd_out's hook and the clear is single-threaded.resetOutput()runs on every exit path ofprocessParsedSingleQuery(including theSCOPE_EXIT_SAFEthat already parks the formatting threads) and at the start of the next query, so the hook is still always removed andstd_out(which outlives a single query) never carries a stale hook into the next query. The pager andINTO OUTFILE ... AND STDOUTbuffers are per-query and destroyed inresetOutput()after the collector join, so they were never affected.The change is confined to
src/Client/ClientBase.cppand does not touch the low-levelWriteBufferFromFileDescriptor(which is used server-wide on a hot single-threaded write path).