Repository navigation
Fix TOCTOU race in vertical merge cancellation - #99628
Conversation
|
Workflow [PR], commit [5e0db44] Summary: ✅ AI ReviewSummaryThis PR fixes a TOCTOU race in Missing context
ClickHouse Rules
Final Verdict
|
|
Thanks @CurtizJ |
|
|
||
| # Start OPTIMIZE in background – it will begin merging as soon as we | ||
| # unblock merges. | ||
| $CLICKHOUSE_CLIENT -q "OPTIMIZE TABLE ${TABLE} FINAL" &>/dev/null & |
There was a problem hiding this comment.
Maybe you don't want to call optimize or optimize final at every loop?
There was a problem hiding this comment.
Okay, you are right. I'll check soon.
There was a problem hiding this comment.
I mean you keep the loop, but not call optimize at every iteration
|
@PedroTadim I've changed. Can you please review this change? |
|
Hi @PedroTadim I can't find what the issue is. I think my change is good. Could you please review this PR one more time? |
|
Thanks @alexey-milovidov @PedroTadim |
One of the runs says: |
Replace the bash test with a single SQL file to eliminate per-statement `clickhouse-client` subprocess overhead that caused timeouts under sanitizers. The test logic is preserved: accumulate parts with 13 columns (vertical merge), rapidly toggle stop/start merges, then verify data integrity. #99628 (comment) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…aration Replace the fragile cancellation-latching approach with a cleaner detection: in `prepareVerticalMergeForAllColumns`, check if `rows_sources_count == 0` with multiple source parts (and rows not fully accounted for by TTL filtering). This means the horizontal stage was effectively cancelled by a racing SYSTEM STOP/START MERGES, so throw `ABORTED` cleanly instead of hitting the `rows_sources` assertion. Also add `no-random-merge-tree-settings` and `no-random-settings` tags to the test to prevent the flaky checker from overriding critical table settings like `vertical_merge_algorithm_min_rows_to_activate`. #99578 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Hi @PedroTadim @alexey-milovidov I've done. |
Avogar
left a comment
There was a problem hiding this comment.
The PR's rows_sources_count == 0 check only catches one symptom of the TOCTOU race. The same race exists in two more places:
-
Partial horizontal writes: if
is_cancelled()fires after some blocks were already processed,rows_sources_count > 0but incomplete — the new guard doesn't fire, and the existingLOGICAL_ERRORat line 1312 still triggers. -
Vertical per-column stage:
executeVerticalMergeForOneColumn(line 1585) checksisCancelled, returns false, thenfinalizeVerticalMergeForOneColumncallscheckOperationIsNotCanceled(line 1601) which sees the blocker cleared → proceeds → hits "Written 0 elements of column v1, but 1000 rows of PK columns". Running the PR's own test 1000 times in 16 threads reproduces this.
The root cause is that both isCancelled and checkOperationIsNotCanceled read the blocker's live atomic counter, and SYSTEM START MERGES can clear it between the two reads. The fix is to latch the cancellation at the two decision points:
// Horizontal stage (executeImpl, ~line 1218):
bool cancelled = ctx->is_cancelled();
if (cancelled || !global_ctx->merging_executor->pull(block))
{
if (cancelled)
global_ctx->merge_list_element_ptr->is_cancelled.store(true, std::memory_order_relaxed);
finalize();
return false;
}
// Vertical per-column stage (executeVerticalMergeForOneColumn, ~line 1585):
bool cancelled = global_ctx->isCancelled();
if (cancelled || !ctx->executor->pull(block))
{
if (cancelled)
global_ctx->merge_list_element_ptr->is_cancelled.store(true, std::memory_order_relaxed);
return false;
}Since checkOperationIsNotCanceled already checks merge_list_element_ptr->is_cancelled, the latch ensures it reliably throws ABORTED regardless of the blocker's current state. This closes the TOCTOU window for all cases, and the rows_sources_count == 0 check becomes unnecessary.
The previous `rows_sources_count == 0` check only caught one symptom of the TOCTOU race between SYSTEM STOP/START MERGES. Two more cases remained: partial horizontal writes and the vertical per-column stage. The root cause is that `isCancelled` and `checkOperationIsNotCanceled` read the blocker's live atomic counter, and SYSTEM START MERGES can clear it between the two reads. Fix this by latching the cancellation at the two decision points: - `executeImpl`: capture `ctx->is_cancelled()` once, persist into `merge_list_element_ptr->is_cancelled` before calling `finalize` - `executeVerticalMergeForOneColumn`: capture `global_ctx->isCancelled()` once, persist before returning Since `checkOperationIsNotCanceled` already checks `merge_list_element_ptr->is_cancelled`, the latch ensures it reliably throws ABORTED regardless of the blocker's current state. The `rows_sources_count == 0` check becomes unnecessary and is removed. #99578 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 95.24% (20/21, 0 noise lines excluded) |
|
Hi @Avogar Could you review this again? |
|
Thanks @Avogar @rschu1ze @PedroTadim |
…erge The merge loop leaves on `is_cancelled`, then `finalize` re-reads the action blocker in `checkOperationIsNotCanceled`. The blocker is a counter, so a `SYSTEM START MERGES` between the two reads makes the recheck pass and the merge commits a prefix of the rows with `error = 0`. The horizontal path has no row-count assertion, so the data loss is silent. Latch the observed cancellation into `merge_list_element_ptr->is_cancelled` before leaving the loop, in both the horizontal and the vertical stage, so the recheck throws `ABORTED` (236) and the merge is retried. Backport of ClickHouse#99628 (a7425a1, first in 26.4.1). Related: ClickHouse#99578 Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Mikhail Filimonov <[email protected]>
Stable 25.8 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Antalya 26.3 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Antalya 25.8 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Stable 26.3 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Stable 24.8 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Stable 25.3 Backport of ClickHouse#99628 - Fix TOCTOU race in vertical merge cancellation
Closes #99578
Summary
A TOCTOU race between
SYSTEM STOP MERGESandSYSTEM START MERGEScould let a vertical merge continue after the horizontal stage processed zero rows, triggering:The
is_cancelled()check inexecuteImpland thecheckOperationIsNotCanceledcheck infinalizecould see different states of the global merges blocker if it was cleared between the two calls. Fix by latching the cancellation into the per-taskis_cancelledflag so thatfinalizereliably throws.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a rare logical error exception during vertical merge when
SYSTEM STOP MERGESandSYSTEM START MERGESare executed concurrently.Documentation entry for user-facing changes
No documentation changes needed (bug fix only).
Version info
26.4.1.52