Repository navigation
Deactivate the background tasks in the MergeTreeData destructor - #113722
Conversation
ThreadSanitizer caught a data race between `MergeTreeData::refreshStatistics` running in the background schedule pool and `MergeTreeData::~MergeTreeData`: the task captures `this` and writes `cached_estimator` under `stats_mutex`, but both members are declared after `refresh_stats_task`, so they are destroyed before the holder's destructor deactivates the task. `shutdown` deactivates the task too, but a task activated after the shutdown (a table startup or an ALTER of `refresh_statistics_interval` racing with a drop) can still be running when the storage is destroyed. `refresh_parts_task` has the same hazard with `refresh_parts_mutex`. Deactivate both tasks explicitly in the destructor before any member is destroyed. Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109453&sha=f2faebe867fbdc127a107ee44f3c369cca2ce04d&name_0=PR&name_1=Stress%20test%20(arm_tsan) PR: #109453 Co-Authored-By: Claude Fable 5 <[email protected]>
…esh-task-destruction-race
|
Workflow [PR], commit [d9169d4] Summary: ✅
AI ReviewSummaryThis PR changes Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 8/10 (80.00%) · Uncovered code |
…structor `outdated_data_parts_loading_task` and `unexpected_data_parts_loading_task` have the same declaration-order hazard as the refresh tasks: their holders are declared before `outdated_unloaded_data_parts` / `unexpected_data_parts`, and `startup` can re-activate them after `shutdown` already deactivated them. Call `stopOutdatedAndUnexpectedDataPartsLoadingTask` in `~MergeTreeData` so the cancellation flags are set before the join and a running load exits early. Addresses the AI review blocker. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 The three stress-test failures on
|
MergeTreeData destructorMergeTreeData destructor
|
The
One residual I am noting rather than raising: the guard is per part, while the observed frame was inside the per-column body (the Your other two bullets need nothing from me: the |
…esh-task-destruction-race
|
🕵 The single red on Opened #113960 to widen the test's timing margins. Also merged the latest |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 37 s compile time in total, 7 of them have a recent master baseline. |
|
🕵 The single red on |
The test distinguishes "the drop cancelled the blocked refresh planning" from "the drop waited for the planning to finish" by racing a 10-second client timeout against a ~30-second `sleepEachRow` chain inside the refresh's planning stage. On a loaded sanitizer runner 10 seconds is not enough for a legitimately cancelled drop: the test failed once with "drop did not finish" on `Stateless tests (amd_tsan, s3 storage)` and then passed 8/8 reruns with the same randomized settings. Widen both sides of the margin: the planning block now lasts ~120 seconds and the drop gets 60 seconds, keeping the discrimination (a drop that waits for planning cannot fit in the timeout) while tolerating scheduling delays. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113722&sha=9e401fcfde10f841ae373a205bc2b1630dd5a48e&name_0=PR Related PR: ClickHouse#113722 Co-Authored-By: Claude Fable 5 <[email protected]>
…esh-task-destruction-race
|
🕵 The only red on |
The tables based on `StorageSystemPartsBase` (`system.parts`, `system.parts_columns`, `system.projection_parts`, `system.projection_parts_columns`) build the whole result eagerly in `initializePipeline`, so a cancelled or timed out query kept building rows over every storage and part until the very end. In a stress test with ThreadFuzzer this took minutes and tripped the hung check: a `SELECT` over `system.parts_columns` stayed in the process list for 252 seconds with `is_cancelled = 1` and `max_execution_time = 10`. Check the query status per storage and per part. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113722&sha=ceedb772f6aa36e221d25ce48f9af71f032a7b08&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29 PR: ClickHouse#113722 Co-Authored-By: Claude Fable 5 <[email protected]>
ThreadSanitizer caught a data race between
MergeTreeData::refreshStatisticsrunning in the background schedule pool andMergeTreeData::~MergeTreeData: the task capturesthisand writescached_estimatorunderstats_mutex, but both members are declared afterrefresh_stats_task, so they are destroyed before the holder's destructor deactivates the task.shutdowndeactivates the task too, but a task activated after the shutdown (a table startup or anALTERofrefresh_statistics_intervalracing with a drop) can still be running when the storage is destroyed.refresh_parts_taskhas the same hazard withrefresh_parts_mutex, andoutdated_data_parts_loading_task/unexpected_data_parts_loading_taskhave it withoutdated_unloaded_data_parts/unexpected_data_parts(startupcan re-activate them aftershutdownalready deactivated them). Deactivate all four tasks explicitly in the destructor before any member is destroyed; for the part-loading tasks this goes throughstopOutdatedAndUnexpectedDataPartsLoadingTask, which sets the cancellation flags first so a running load exits early.Caught by
Stress test (arm_tsan)on an unrelated pull request: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109453&sha=f2faebe867fbdc127a107ee44f3c369cca2ce04d&name_0=PR&name_1=Stress%20test%20(arm_tsan)The race is inherently non-deterministic (a stress-test interleaving of a table drop with the background statistics refresh), so no deterministic regression test is added; the CI report above is the reproduction.
Related: #89544
Related: #109453
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a data race between the background tasks of a
MergeTreetable (statistics refresh, parts refresh, outdated/unexpected parts loading) and the destruction of the table.