Repository navigation
Conversation
`ReplicatedMergeTreeRestartingThread::run` must not run on two threads at once, but it has two independent executors: its own `BackgroundSchedulePool` task, whose executions are serialized by `BackgroundSchedulePoolTaskInfo::exec_mutex`, and the direct inline `restarting_thread.run` the attach path takes at `StorageReplicatedMergeTree.cpp:5979`, which bypasses the task and therefore that mutex. `BackgroundSchedulePoolTaskInfo::deactivated` defaults to `false`, so the task is schedulable from the moment it is created. That lets the inline activation be overtaken by its own side effects: `runImpl` arms `queue_updating_task`, the ZooKeeper session then expires, `queueUpdatingTask` catches `ZSESSIONEXPIRED` and calls `restarting_thread.wakeup`, and a pool thread enters `run` while the inline executor is still between `setNotReadonly` and `first_time = false`. It observes `first_time == true` with `is_readonly == false`, which is the state `chassert(storage.is_readonly)` rules out. Creating it deactivated is the convention the storage constructor already states at `StorageReplicatedMergeTree.cpp:481`, "We create and deactivate all tasks for consistency. They all will be scheduled and activated by the restarting thread.": the four tasks created there are all deactivated there, `ReplicatedMergeTreePartCheckThread` does the same in its own constructor, and a dozen further tasks across `src/` are created that way. So this restores an existing convention rather than adding a second lock to tolerate its absence. Three of the storage's other tasks are created active (`cleanup_thread` and `deduplication_hashes_cache`, both started by the restarting thread, and `part_moves_between_shards_orchestrator`, started by `startupImpl`); none of them is ever run inline, so none has a second executor to overlap, and none is touched here. The refused wakeup is not lost. Immediately after the inline call the attach path runs `restarting_thread.start(/*schedule=*/ true)`, which activates and schedules, so the pool re-enters `run` with `first_time` already cleared and takes the `getZooKeeper()->expired()` branch, performing a proper `partialShutdown` and restart. That ordering is what `8ff11ef56512d` established; on a branch where the attach path still calls `start(/*schedule=*/ false)` this change would turn the coalesced retry into a dropped one, so the two belong together. Reported as `Logical error: 'storage.is_readonly'` in `Stress test (arm_debug)` on ClickHouse#117293. The CI report for that run is https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=1209779bd81c5de44849dc0908019d0bda7b670a&name_0=PR&name_1=Stress%20test%20%28arm_debug%29 The assertion is debug and sanitizer only; a release build continues with both executors inside `setZooKeeper` and `tryStartup` on the same table. The regression test parks the inline activation on a new `PAUSEABLE_ONCE` failpoint and expires the session with `SYSTEM RECONNECT ZOOKEEPER`, so the interleaving is forced rather than waited for. While the activation is parked it also polls `system.background_schedule_pool`, where a deactivated task has no row, so the refused schedule is observed rather than inferred, and once the table has recovered the same query is required to return a row, so the poll cannot pass by matching nothing. It aborts the server in 5 of 5 runs without the one-line change and passes 20 of 20 with it. It needs the default `background_schedule_pool_size`, so it cannot live in `test_replicated_table_attach`, which pins that to 1. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Four rounds of an independent cold review plus a second-model gate (engine: codex), on
Session id: cron:clickhouse-review-slot-48:20260910-150700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260910-141400 |
|
Workflow [PR], commit [6749d9a] Summary: ❌
AI ReviewSummaryThis PR makes the Final Verdict
|
|
cc @CheSema @alesapin, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 25 s compile time in total, 9 of them have a recent master baseline. |
CI finish ledger - 6749d9aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
This job started 17:16:55Z, about 10 minutes before #119298 merged, so the build predates the Not caused by this PR: the diff is Session id: cron:our-pr-ci-monitor:20260910-200115 |
|
One more CI hit of this race, this time in a release build and with a worse symptom: The server log shows the same interleaving on the attach path (container time):
So in a release build the second |
|
@groeneai is this PR still needed? |
|
Yes. Master still has the race this fixes: the restarting thread's task is still created schedulable, the attach path still activates the replica inline with CI hit it twice so far: The PR still merges cleanly. Its CI is from 2026-09-10, so I can merge master to rerun it if you want. |
|
A third CI hit, the first on master: The trigger is different from the session expiry, and more common: only a failed inline activation is needed. For With the task created deactivated, that |
Related: #117293
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a race during
ReplicatedMergeTreestartup on the attach path: a ZooKeeper session expiring during replica activation could runReplicatedMergeTreeRestartingThread::runon a background pool thread concurrently with the inline activation. The observed effect is an assertion abort (Logical error: 'storage.is_readonly') in debug and sanitizer builds; a release build instead re-runssetZooKeeperandtryStartupmid-activation.Description
ReplicatedMergeTreeRestartingThread::runhas two independent executors and must not run on both at once: its ownBackgroundSchedulePooltask, serialized byexec_mutex, and the inlinerestarting_thread.runthe attach path takes (StorageReplicatedMergeTree.cpp:5979), which bypasses both.BackgroundSchedulePoolTaskInfo::deactivateddefaults tofalse, so the task is schedulable from construction and the inline activation can be overtaken by its own side effects:runImplarmsqueue_updating_task, the session expires,queueUpdatingTaskcatchesZSESSIONEXPIREDand callsrestarting_thread.wakeup, and a pool thread entersrunwhile the inline executor is still betweensetNotReadonlyandfirst_time = false. It readsfirst_time == truewithis_readonly == falseand tripschassert(storage.is_readonly).Creating the task deactivated is the convention the storage constructor already states at
StorageReplicatedMergeTree.cpp:481: the four tasks created there are all deactivated there, andReplicatedMergeTreePartCheckThreaddoes the same in its own constructor. It makes thewakeupin that window a no-op, and the attach path's followingstart(/*schedule=*/true)activates and schedules, so it is coalesced, not lost.Requested by @ alexey-milovidov in #117293 (comment).
A new integration test parks the inline activation at a failpoint and expires the session with
SYSTEM RECONNECT ZOOKEEPER, forcing the interleaving. While parked it pollssystem.background_schedule_pool, where a deactivated task has no row, so the refused schedule is observed rather than inferred; the same query must return the row again after recovery. Without the one-line change it aborts the server in 5 of 5 runs with the stack from the CI report; with it, 20 of 20 pass, and in the 10 runs whose server log I counted the refusedwakeupis coalesced into a normal session-replacement restart in all 10.test_replicated_table_attachstill passes 6 of 6, so no wakeup is dropped.Neighbouring races left untouched, and CIDB breadth for this signature
Three neighbouring races are untouched, none reachable for this assertion, none with a CIDB occurrence:
session_expired_callback_handleroutliving a failed attach attempt,is_readonly_metric_setwritten from both threads, andshutdownnot waiting for an inline execution.Exactly one occurrence in 90 days, and no open issue or pull request matches the signature, so this is not verifiable post-merge by a drop in failure frequency; the evidence is the reddening negative arm plus the call-site enumeration.
Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=1209779bd81c5de44849dc0908019d0bda7b670a&name_0=PR&name_1=Stress%20test%20%28arm_debug%29
Workflow [PR]
Sync PR [sync-upstream/pr/119264]