Repository navigation
Avoid a one-minute shutdown delay after writing an access entity to disk - #118099
Conversation
…sStorage DiskAccessStorage writes its .list files from a background thread that first waits up to a minute so that several list updates can be batched. stopListsWritingThread asked that thread to exit by calling notify_one on lists_writing_thread_should_exit while holding no lock and recording no state, and the thread waited on the plain wait_for overload with no predicate. A notify_one with no waiter armed is a no-op, so whenever the thread had not yet taken mutex and entered its wait, the request was lost: the thread waited out the full minute, and join() held shutdown open until it did. clickhouse-local reaches this most easily, because an access-entity DDL is usually the last thing the process does, so shutdown starts about a millisecond after the thread is created. Measured on a debug build, 6144 invocations of "CREATE SETTINGS PROFILE ... SETTINGS http_response_headers" at 32-way concurrency with default settings: 5 processes took just over 60 seconds and nothing at all took between 30 and 55 seconds. In CI the same event kills 04902_access_entity_map_setting_round_trip at the 60 second per-test cap of Fast test, which then drops the rest of the matrix; in the suites whose cap is far above a minute it is instead recorded as a slow pass, 44 times in 7 days against 24946 runs. Record the request in a mutex-guarded flag before notifying, and give the wait_for the predicate that reads it. Because the predicate is checked before the thread blocks, a request that arrived early is observed instead of lost. The same line previously let a spurious wakeup return early and skip writeLists(), which the predicate also settles: only a real exit request returns early now, and a timeout falls through to the write as intended. The flag is never reset because shutdown() is terminal, and mutex is released before join() since listsWritingThreadFunc acquires it. All of CREATE, ALTER and DROP of a user, role, settings profile, row policy and quota are covered, since insertNoLock, removeNoLock and updateNoLock share one scheduleWriteLists. The behaviour dates from 2408c8c (2020-02-27). Co-Authored-By: Claude Opus 5 <[email protected]>
…meout Insert an entity and then shut the storage down immediately. In that ordering the request to stop the background lists-writing thread is issued before the thread has started waiting for it, which is the interleaving that used to lose the request. Writing the lists takes a millisecond, so a 20 second bound separates a healthy shutdown from one that waits out the one-minute batching timeout by a wide margin in both directions. A stateless test cannot force this: from a shell only server settings and machine load are available, and the one setting that makes the ordering deterministic, max_thread_pool_size=1, makes that single pool thread the bottleneck for the rest of shutdown instead, so it stalls either way. Measured on a debug build: 20 of 20 iterations fail without the preceding commit, each reporting "shutdown() took 60000 ms", and 20 of 20 pass with it in 1 ms. Co-Authored-By: Claude Opus 5 <[email protected]>
…eadable The stalling interleaving is not guaranteed on a single pass: if the background thread reaches its wait before the stop request is issued, even the unfixed code delivers the request and shuts down promptly. Twenty iterations, each with its own directory and storage, make one lucky ordering insufficient, and the bound is a fatal assertion so the unfixed build pays one one-minute wait rather than twenty. Checking that need_rebuild_lists.mark is absent and users.list exists could not distinguish a shutdown that wrote the lists from one that never scheduled them, because the constructor's recovery path creates users.list before the code under test runs. Reopen the directory and resolve the inserted user instead: with the marker absent the second storage reads the .list files rather than rebuilding from the <id>.sql files, so it finds the user only if users.list names her. Making scheduleWriteLists return immediately passes the old assertions and fails this one. Correcting the scope stated in the preceding commit message: creating or dropping any of the six access entity types always schedules a list write, while an ALTER does so only when it renames the entity, and the six include masking policies. Measured on a debug build: 400 iterations pass with the fix, the slowest shutdown() taking 1 ms; the unfixed build fails on the first iteration of all 5 runs with "shutdown() took 60000 ms". Co-Authored-By: Claude Opus 5 <[email protected]>
The comment said which of the two orderings wins varies per pass. That is not what the measurement shows: on a debug build the unfixed code stalled on the very first iteration in all 25 samples taken, so on this machine the order is in practice stable. The property the loop relies on is the weaker and correct one, that neither ordering is guaranteed, which is why one pass is not a reliable probe. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model reviewReviewed cold against the code, then adjudicated. The ❌ The test could have passed on master by luck. Without the fix ❌ The test passed even with list scheduling neutered. Checking that 💡 The other 💡 Noted, not fixed: these commit messages should have wrapped identifiers in inline code blocks per |
|
Workflow [PR], commit [61af973] Summary: ✅
AI ReviewSummaryThis PR fixes a lost wakeup in PR Metadata
Final Verdict
|
|
cc @vitlibar, 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, 29 s compile time in total, 9 of them have a recent master baseline. |
CI finish ledger - 2174b39Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
This head carries no failing test. The suite was killed mid-run, so its per-test rows never reached
Session id: cron:our-pr-ci-monitor:20260904-200000 |
|
Thanks for the report. It is the same mechanism this PR fixes, so it can keep happening until this lands.
Your build cannot carry the fix: master still has the unpredicated wait ( In practice it is rare (CIDB, last 30 days: 6 failures against 117,394 OK), and |
…rage-shutdown-stall Resolve the conflict in `src/Access/tests/gtest_disk_access_storage.cpp` by keeping both sides: the recovery tests added on master and the shutdown test of this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The helper was added to the same test file on master. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
clickhouse-localandclickhouse-serverno longer take an extra minute to exit when the last operation before shutdown created, dropped or renamed an access entity (a user, role, settings profile, row policy, quota or masking policy) in thelocal_directoryaccess storage.Description
DiskAccessStoragewrites its.listfiles from a background thread that first waits up to a minute, so that several list updates can be batched.stopListsWritingThreadasked that thread to exit by callingnotify_oneonlists_writing_thread_should_exit, holding no lock and recording nothing. If the thread had not reached itswait_foryet, the notification was dropped, the thread waited out the whole minute, andjoinheld shutdown open for it.clickhouse-localreaches this most easily, because the DDL is usually the last thing the process does, so shutdown starts about a millisecond after the thread is created.I now record the request in a
mutex-guarded flag before notifying, and pass thewait_forthe predicate that reads it. The same line previously let a spurious wakeup return early and skipwriteLists, which this also settles.CREATEandDROPalways schedule a list write andALTERdoes when it renames, so every access entity type reaches this through the singlescheduleWriteLists. The constructor andSYSTEM RELOAD USERSwrite the lists inline and start no thread. The code is from 2020.In CI it appears as
04902_access_entity_map_setting_round_tripbeing killed at the 60 s per-test cap inFast test, which drops the rest of the matrix: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116793&sha=a1ada61eaceb3825f95652b597d18c8920b07f5e&name_0=PR&name_1=Fast%20testValidation: the new unit test fails on master with
shutdown() took 60000 msand passes 400 iterations with the fix, the slowestshutdowntaking 1 ms. End to end, 6144clickhouse-localinvocations at 32-way concurrency with default settings gave 5 runs over 55 s before the fix and 0 after, with nothing between 30 s and 55 s in either arm.A shell test cannot force this interleaving: the one setting that makes it deterministic (
--max_thread_pool_size=1) makes that single pool thread the bottleneck for the rest of shutdown, so it stalls with the fix too.The same stall is already visible in CI as a slow pass (click to expand)
In the suites whose per-test timeout is far above 60 s, the same event is recorded as a slow
OK:44 runs at baseline plus a minute, and not a single run in the whole 30 s to 55 s band. Load
produces a continuum; one fixed timeout produces a gap and a cluster. For
Fast testitself thesame 7 days give 4268 passing runs with p50 2760 ms, p90 3310 ms and a maximum of 5960 ms, so the
two 60 s failures are ten times the slowest pass ever observed.
Workflow [PR]
Sync PR [sync-upstream/pr/118099]
Version info
26.11.1.1009-master(included in26.11and later)