Repository navigation
Stop the Pretty squash thread when its output format is cancelled - #129825
Conversation
With `output_format_pretty_squash_consecutive_ms != 0`, `PrettyBlockOutputFormat` renders squashed blocks from a background thread under `writing_mutex`. Owners of the output buffer cancel the format and then the buffer (`StorageFileSink::cancelBuffers`), relying on a cancelled format no longer touching the buffer, which is what `ParallelFormattingOutputFormat::onCancel` guarantees by joining its collector. `PrettyBlockOutputFormat` had no `onCancel`, and the thread wrote on any wake-up of `wait_for` without re-checking `finish`. A failed `INSERT INTO FUNCTION file(...)` with a queued block therefore cancelled the buffer, destroyed the format, and the destructor's `stopThread` wake-up rendered the block into the cancelled buffer: a data race reported by TSan in `La Casa Del Dolor (amd_tsan)` (STID 1003-46d8). The thread's own timeout firing between the buffer cancel and the destructor is a second way into the same race. `onCancel` now sets `finish` under `writing_mutex`, and the thread re-checks `finish` after every wake-up. Each change alone leaves one of the two paths racing. No join in `onCancel`: it can run on the writer thread itself, when its write error cancels the query. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first case of 05564 only reaches the destructor's wake-up of the squash thread, so it stays green when `PrettyBlockOutputFormat::onCancel` is removed and only the `finish` re-check is left. The second case queues a 100k-row block that the thread starts writing right away, and throws from the next block 0.8 s later, while the write is still in progress. Without `onCancel` the sink cancels its buffer under the writing thread: TSan reports the race (and a single-character write into the cancelled buffer aborts on `LOGICAL_ERROR`). With it, cancelling the format waits for the write to end. Measured on TSan: base 9/10 red, the `finish` re-check alone 10/10 red, the fix 0/10; the whole test is green 10/10 on the fix with randomized settings, and `onCancel` alone still fails the first case. On other builds the write ends before the cancel, so the case is a smoke test there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the onCancel comment to its postcondition. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds): 1 finding accepted and fixed in the test, no open findings
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261106-175700 |
|
cc @PedroTadim @CheSema, could you review this and add the |
|
Workflow [PR], commit [0a2af49] Summary: ✅
AI ReviewSummaryThe PR makes Missing context / blind spots
Final VerdictStatus: ✅ Approve |
|
@alexey-milovidov feel free to merge PRs assigned to me |
|
🕵️ @groeneai The fix looks right to me. One question about the cost the description mentions. |
`writing_mutex` is held by every writing method of `IOutputFormat`, so `onCancel` (and with it `KILL QUERY` and `ProcessList::killAllQueries`) waits for one blocked write to `out`, not only for one squashed render. Measured over HTTP with a client that stopped reading and `http_send_timeout = 10`: `KILL QUERY` returns after 19-20 s instead of 0.1 s, while the query itself ends at the same time as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
|
Thanks, done in 334fe6c: the I measured it over HTTP with a client that stops reading (
So the query does not stop later; |
CI finish ledger - 334fe6cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
No job was dropped, and Session id: cron:our-pr-ci-monitor:20261107-023005 |
Related: #128292
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
An
INSERTintofile()(orFile,url(),s3(), ...) with aPretty*format that fails after its first block has a data race: the format's squash thread renders the queued block into the sink's write buffer after the sink has cancelled it. Found byLa Casa Del Dolor (amd_tsan)on an unrelated PR: report.With
output_format_pretty_squash_consecutive_ms != 0(default),PrettyBlockOutputFormatwrites squashed blocks from a background thread underwriting_mutex. Sinks cancel the format and then its buffer (StorageFileSink::cancelBuffers), relying on a cancelled format no longer touching the buffer, asParallelFormattingOutputFormat::onCancelguarantees.PrettyBlockOutputFormathad noonCancel, and its thread wrote on any wake-up without re-checkingfinish, so the destructor'sstopThread(or the thread's own timeout) wrote into the cancelled buffer.onCancelsetsfinishunderwriting_mutex: when it returns, no write is in progress and none starts later.writingThreadexits when it wakes up withfinishset; the rest is written bywriteSuffix, or not at all for a cancelled format.This covers every owner that cancels the format before its buffer (file, URL, object storage and message queue sinks).
onCancelmay now wait for one blocked write to the buffer, likeParallelFormattingOutputFormat::onCancelwaits for its collector.Validation: the new test
05564_pretty_squash_cancelled_insertreproduces the CI race in 19/20 runs on the master TSan build of the base commit and in none on the fixed build. The test has one case per path, a queued block and a block being written, and each change alone fails one of them (onCancelalone the first 5/5, thefinishre-check alone the second 10/10, on TSan). It passes 50/50 on a debug build, and so do the otherPrettytests. On a release build the query still fails with its own error, hence not for changelog.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129825]
Version info
26.11.1.1805-master(included in26.11and later)