Repository navigation
Fix StorageMergeTree startup/shutdown race - #121671
Conversation
A `DETACH` racing an in-flight async `startup` could leave background work armed on a logically shut-down table: `shutdown` returned early on a repeated call, and `startup` did not re-check `shutdown_called` after arming the refresh tasks, the outdated-parts loading task, the background assignees and the cleanup thread. Since `flushAndPrepareForShutdown` runs only once, nothing would ever stop them, and an armed task could fire while `~MergeTreeData` destroys the state it touches. `shutdown` now publishes `shutdown_called` before deactivating the periodic tasks and, on a repeated call (including the destructor's), stops everything a racing `startup` could have re-armed. `startup` re-checks the flag after arming and unwinds. All stops are idempotent. Split out of #96130 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [8022d00] Summary: ✅
AI ReviewSummaryThis PR hardens the non-replicated Final Verdict
LLVM Coverage ReportMeasured on commit 8022d00.
Changed lines: Changed C/C++ lines covered: 66/71 (92.96%) · Uncovered code |
…tup-shutdown-race
Address review: the fix had no regression coverage. The test runs the losing interleaving deterministically: the complete first `shutdown` (as done by `DETACH`), then the late `startup`, then the repeated `shutdown` (as done by the destructor), and checks that no task of the table stays live in the schedule pools. Without the fix, three tasks stay live in the common pool and one in the streaming pool. Co-Authored-By: Claude Opus 5.5 (1M context) <[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 units242 translation units recompiled, 2560 s compile time in total, 242 of them have a recent master baseline. |
`startStatisticsCache` move-assigns the `refresh_stats_task` holder, and it can run concurrently with `shutdown` (a late async `startup`, or an `ALTER` of `refresh_statistics_interval` racing with a `DETACH`/`DROP`), which reads and deactivates the same holder. The holder is a plain `shared_ptr` wrapper, so this is a data race. Guard the holder with `refresh_stats_task_mutex` and add `stopStatisticsCache`, which deactivates the task and latches `refresh_stats_stopped`, so a `startStatisticsCache` that runs after it does not arm the task at all. Use it in `StorageMergeTree::shutdown`, the `startup` re-check, `StorageReplicatedMergeTree::shutdown` and `~MergeTreeData`. Add a gtest that runs `startup` and `flushAndShutdown` concurrently and checks that no task of the table stays live. Addresses the AI review on #121671 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Merged |
…down` A late async `startup` on a table that was already shut down used to arm the cleanup thread and the part-loading tasks, which run their first iteration right away, and only then unwind them via the post-arm `shutdown_called` re-check. Return before arming anything; the post-arm re-check stays for the case where `shutdown` wins in the middle of `startup`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: Other failures: |
|
The fix is in progress: #123790 (it includes the change from my #123774, closed in its favor). In its own run on It is not flaky: on |
The rollback of a failed writable-table settings `ALTER` calls `wakeupBackgroundWorkers`, which re-arms the outdated/unexpected part loaders via `startOutdatedAndUnexpectedDataPartsLoadingTask`. If `shutdown` has already stopped them, they could fire on a shut-down table. Make `wakeupBackgroundWorkers` a no-op once `shutdown_called` is set, which covers every caller. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`MergeTreeData::changeSettings` calls `startBackgroundMovesIfNeeded` on a `storage_policy` change, which only checked `background_workers_enabled`, so a settings `ALTER` racing after `shutdown` could re-arm `background_moves_assignee` on a shut-down table. Skip it after `shutdown`, re-check `shutdown_called` after `start` to close the race with a concurrent `shutdown`, and make `scheduleDataMovingJob` return early after `shutdown`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed` after arming in `ALTER` `flushAndShutdown` runs `flushAndPrepareForShutdown` before `shutdown_called` is set, so a concurrent `startup` could re-arm the assignees and the cleanup thread after they were stopped without observing the flag, and `shutdown` never stopped them again (`flushAndPrepareForShutdown` runs only once). This was caught by `StorageMergeTree.ConcurrentStartupAndShutdownArmNoBackgroundTasks` in Unit tests (msan): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121671&sha=latest&name_0=PR&name_1=Unit%20tests%20(msan) PR: #121671 Now `shutdown` stops all background tasks unconditionally on every call, after publishing the flag. Address review: the `table_readonly` 1 -> 0 `ALTER` re-checks `shutdown_called` after `startBackgroundWorkers` and stops everything if a concurrent `shutdown` won, and the post-commit disk cleanup is skipped on a shut-down table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… workers `wakeupBackgroundWorkers` checked `shutdown_called` only before the wake-up, so a `shutdown` that published the flag right after that check could still see the cleanup task and the part loaders re-armed on a shut-down table. Re-check the flag after the wake-up and stop all background tasks if a concurrent `shutdown` won, the same pattern as in `startup` and the `table_readonly` `ALTER`. Also skip `MergeTreeCleanupThread::iterate` once `shutdown_called` is set, so an iteration scheduled in that window does nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts with #121672 (the `StorageReplicatedMergeTree` half), which already added `refresh_stats_task_mutex` and `stopStatisticsCache`: keep the `refresh_stats_stopped` latch from this PR on top of it, and take master's `StorageReplicatedMergeTree::shutdown`, which already stops the statistics task. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r-part Conflicts with #121671 resolved by keeping its startup/shutdown race fix in `StorageMergeTree` while dropping the statistics refresh task (`startStatisticsCache`, `stopStatisticsCache`, `refresh_stats_task_mutex`), which this PR removes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the cherry-pick of #125165 to 26.9. The conflicts come from #121671 and #121672, which are not on 26.9: on master the statistics refresh task is stopped through `stopStatisticsCache` under `refresh_stats_task_mutex`, and the `refreshStatistics` loop no longer takes the parts lock per part. On 26.9 the task is deactivated directly in `startStatisticsCache`, `~MergeTreeData` and the `shutdown` of `StorageMergeTree` and `StorageReplicatedMergeTree`, so `refresh_stats_cancelled` is set before each of those four `deactivate` calls, reset when `startStatisticsCache` arms a new task, and checked before each part in `refreshStatistics`. Neither the mutex nor `stopStatisticsCache` is ported, and the `LoadedStatistics` profile event that master's loop also increments is left out. The `05361_statistics_refresh_does_not_block_drop` test is ported unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the cherry-pick of #125165 to 26.8. The conflicts come from #121671 and #121672, which are not on 26.8: on master the statistics refresh task is stopped through `stopStatisticsCache` under `refresh_stats_task_mutex`, and the `refreshStatistics` loop no longer takes the parts lock per part. On 26.8 the task is deactivated directly in `startStatisticsCache`, `~MergeTreeData` and the `shutdown` of `StorageMergeTree` and `StorageReplicatedMergeTree`, so `refresh_stats_cancelled` is set before each of those four `deactivate` calls, reset when `startStatisticsCache` arms a new task, and checked before each part in `refreshStatistics`. Neither the mutex nor `stopStatisticsCache` is ported, and the `LoadedStatistics` profile event that master's loop also increments is left out. The `05361_statistics_refresh_does_not_block_drop` test is ported unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A
DETACHracing an in-flight asyncstartupof aMergeTreetable could leave background work running on a table that was already shut down. A repeatedshutdownreturned early, andstartupdid not re-checkshutdown_calledafter arming the refresh tasks, the outdated-parts loading task, the background assignees and the cleanup thread.flushAndPrepareForShutdownruns only once, so nothing would ever stop them, and an armed task could fire while~MergeTreeDatadestroys the state it touches.With this change,
shutdownsetsshutdown_calledbefore deactivating the periodic tasks. On a repeated call (including the one from the destructor), it stops everything a racingstartupcould have re-armed.startupreturns early if the flag is already set, and re-checks it after arming to undo what it armed ifshutdownwon in the middle. All the stops are idempotent.stopStatisticsCache(added together withrefresh_stats_task_mutexby #121672) now also sets a latch, so that a laterstartStatisticsCache(fromstartupor anALTERofrefresh_statistics_interval) does not arm the statistics task aftershutdown.The unit test
gtest_merge_tree_startup_after_shutdown.cppruns the losing interleaving deterministically (a completeshutdown, thenstartup, then the repeatedshutdown) and checks that no task of the table stays live in the schedule pools. It fails without the fix. A second test runsstartupandflushAndShutdownconcurrently.Scope: this PR covers
StorageMergeTree. TheStorageReplicatedMergeTree/ReplicatedMergeTreeAttachThreadhalf of the same race was fixed in #121672. On current master, the known SQL paths do not runshutdownconcurrently withstartup:DETACHwaits for the table's startup job,ATTACHrunsstartupunder the DDL guard, and database shutdown runsstopLoadingfirst. The race was reached through the randomDETACH/ATTACHhook from #96130.Split out of #96130, where the random
DETACH/ATTACHtesting hook exposed the race.Related: #96130
Related: #121672
Changelog category (leave one):
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121671]
Version info
26.10.1.1892-master(included in26.10and later)