Repository navigation
Release the tasks left in a MergeTree background executor's queue on shutdown - #113653
Conversation
…shutdown `MergeTreeBackgroundExecutor::wait()` set `shutdown = true` and waited for the running jobs, but never touched the `pending` queue. `threadFunction` breaks out of its loop before popping once `shutdown` is set, so a task that `routine` had re-pushed for another step was neither resumed nor cancelled. `MergePlainMergeTreeTask` is multi-step, and it holds a `TableLockHolder` (a table share lock taken with `NO_QUERY`) for as long as the task object lives. An abandoned task therefore keeps that lock until the storage's own shutdown reaches `removeTasksCorrespondingToStorage`, and any exclusive table lock taken in between waits the full `lock_acquire_timeout` and then fails with `DEADLOCK_AVOIDED`. On the upgrade path this is what breaks the rotation of `system.session_log`. Because that log is written only on login and logout, its first flush of the run lands after `Shutting down storages`, so its obsolete-structure `RENAME` runs inside the window: `prepareTable` then fails once the lock acquisition times out (60 s in the Upgrade check, which installs `tests/config/users.d/limits.yaml`; the default is 120 s), and `flushImpl` still confirms the batch, so the rows are dropped. It became reachable for every upgrade from a released build when `ArrowFlight` was added to the `interface` enumeration. In the occurrence this was diagnosed from, the lock was released 2 s after the rename had already timed out. Drain `pending` in `wait()`, cancelling and destroying each remaining task. The drain runs after `pool->wait()`, which is the point from which no worker can push to `pending` any more: `--scheduled_jobs` happens only after a job function returns, and each `threadFunction` loop is one pool job. Draining only before `pool->wait()` is measurably insufficient, because a worker still mid-step re-pushes afterwards. It is invoked through `SCOPE_EXIT` so that a worker exception rethrown by `ThreadPool::wait()` cannot skip it. No task that would otherwise have completed is cancelled, since the workers had already stopped popping. This covers all four executors and every storage that uses them, not only system logs. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (click to expand)Two review rounds by a model that did not write the change, each followed by an independent
Residual, stated rather than papered over: the arm that exercises the drain's placement is Round 1's findings, for completeness: the description quoted the Upgrade check's 60 s |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-45:20260806-092200 |
|
cc @alesapin @azat, could you review this? |
|
Workflow [PR], commit [2120202] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict
LLVM Coverage ReportMeasured on commit 2120202.
Changed lines: Changed C/C++ lines covered: 51/53 (96.23%) · Uncovered code |
CI finish ledger - 2a033adEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Merged PR #113409, which added that retry, is deliberately not linked as the owner: the eight Session id: cron:our-pr-ci-monitor:20260806-213000 |
Master factored `requestShutdown` out of `wait` in 3e1b29b, which touched the same lines as this branch. `wait` now calls it and the `pending` drain still runs from a `SCOPE_EXIT` around `pool->wait()`, the point past which no worker can push there any more. `requestShutdown` alone does not release the pending tasks, so the leak this branch fixes is unchanged. Co-Authored-By: Claude Opus 5 <[email protected]>
The test scheduled a single task, so replacing the drain's loop with one pop would have left tasks alive and still passed. Schedule three cycling tasks and assert that each of them is cancelled and destroyed. Co-Authored-By: Claude Opus 5 <[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 units333 translation units recompiled, 3388 s compile time in total, 333 of them have a recent master baseline. |
CI finish ledger - 013e209Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose
No other check failed at this head, and none of the failures is PR-caused: the diff here touches Session id: cron:our-pr-ci-monitor:20260830-190000 |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The changes are good, except the unit test. We prefer not to use unit tests. But still acceptable.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a shutdown stall where a background merge or mutation left in a
MergeTreebackground executor's queue kept holding its table lock, so any table lock taken later during shutdown waited for the fulllock_acquire_timeout(120 s by default) and then failed withDEADLOCK_AVOIDED. Observed assystem.session_loglosing a batch of rows when its schema is rotated on upgrade.Description
MergeTreeBackgroundExecutor::wait()flipped the executor into shutdown mode and waited for the running jobs, but never touched thependingqueue.threadFunctionbreaks out of its loop before popping onceshutdownis set, so a task thatroutinehad re-pushed for another step (MergePlainMergeTreeTaskis multi-step) was neither resumed nor cancelled. Such a task keeps itsTableLockHolder, a table share lock taken withNO_QUERY, for as long as the task object lives, i.e. until the storage's own shutdown reachesremoveTasksCorrespondingToStorage.Any exclusive table lock taken between those two points therefore waits the full timeout. On the upgrade path this hits
system.session_log: written only on login/logout, its first flush of the run lands afterShutting down storages, so its obsolete-structureRENAMEruns inside the window.prepareTablethen fails, andflushImplstill confirms the batch, so the rows are dropped. In the reference occurrence the lock was released 2 s after the rename had timed out.The fix drains
pendinginwait(), cancelling and destroying each remaining task. The drain runs afterpool->wait(), which is the point from which no worker can push topendingany more (--scheduled_jobshappens only after a job function returns, and eachthreadFunctionloop is one job), and viaSCOPE_EXITso a rethrown worker exception cannot skip it. No task that would otherwise have completed is cancelled, because the workers had already stopped popping. All four executors and every storage using them are covered, not just system logs.Validated with a new gtest in the existing
gtest_executor.cpp: it fails on unmodifiedwait()and passes with the fix. Four mutants each redden only that test: removing the drain, draining without cancelling, draining only beforepool->wait(), and draining a single task instead of the whole queue.Related: #112951
Workflow [PR]
Sync PR [sync-upstream/pr/113653]
Version info
26.10.1.464-master(included in26.10and later)