Repository navigation
Fix broken_promise abort when scheduling the final S3 multipart upload task fails - #108730
Conversation
…d task fails TaskTracker::addFinal stores the future of the final task (the async S3/Azure completeMultipartUpload) in `futures` before the task is scheduled. When the scheduler (threadPoolCallbackRunnerUnsafe -> scheduleOrThrowOnError) threw CANNOT_SCHEDULE_TASK -- which the stress test injects via the thread fuzzer, and which can also happen on a genuinely exhausted pool -- the final packaged_task was destroyed without running. That left the stored future with a broken promise, so waitAll() threw std::future_error (a std::logic_error) instead of the real CANNOT_SCHEDULE_TASK. In debug and sanitizer builds getCurrentExceptionMessage aborts on any std::logic_error, so a background merge that wrote its result to S3 aborted the server. Both schedule sites (the SCOPE_EXIT in add() and the run-now branch in addFinal()) now go through scheduleFinalTask(), which runs the final task inline if scheduling fails. The task's future then always carries a proper result or the actual exception, so waitAll() surfaces the real error and the background merge is retried normally instead of aborting. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260628-040200 |
|
cc @asya-ch @kssenii — could you review this? It fixes the |
|
Workflow [PR], commit [ef5ba23] Summary: ✅
AI ReviewSummaryThis PR replaces the final Final Verdict
|
…duleFinalTask The Style check flagged the catch (...) in TaskTracker::scheduleFinalTask as silently swallowing exceptions. The catch is intentional: when the scheduler throws (e.g. CANNOT_SCHEDULE_TASK from thread-fuzzer fault injection) we run the final packaged_task inline so its future is always fulfilled, rather than letting it be destroyed unrun and turn into a broken promise. The catch_all linter inspects only the catch body and the two lines preceding it, so the detailed rationale that lived above the try was out of range; move a concise justification (with an 'Ok' marker the linter accepts) into the catch body. Comment-only change, no behavior change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ing inline
The previous fix ran the final task inline when scheduling it threw. That
masked the real scheduling error: the inline run succeeded, so the stored
future succeeded too, and waitAll() never reported the CANNOT_SCHEDULE_TASK
that triggered the path. It also left a second gap: if the pool accepted the
final lambda and then dropped it unrun during shutdown, the discarded scheduler
future could not satisfy the stored future, and destroying the captured
packaged_task re-introduced the std::future_error broken_promise.
Introduce a small FinalTaskState (an explicit promise plus the callback,
mirroring detail::CallbackRunnerTask) whose future is the one stored in
`futures`. The promise is satisfied on every outcome:
- run() sets value/exception from the callback;
- a pre-enqueue scheduler throw satisfies it with the scheduling exception,
so waitAll() surfaces CANNOT_SCHEDULE_TASK instead of a masking success;
- a queued-drop (state released without run()) satisfies it from
~FinalTaskState with a normal CANNOT_SCHEDULE_TASK Exception, never a
broken-promise std::future_error.
Rework the gtests accordingly: the schedule-failure cases now assert waitAll()
observes CANNOT_SCHEDULE_TASK (not success, not std::future_error), covering
both the addFinal run-now site and the async SCOPE_EXIT site, plus a
deterministic queued-drop case. All three fail on the old inline-run code and
pass with this change.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260628-215700 |
CI finish ledger — 97bdc98Every failure below has an owner: a fixing PR (ours or external). Only
Session id: cron:our-pr-ci-monitor:20260701-120000 |
…-schedule-failure
Addresses clickhouse-gh[bot] review on scheduleFinalTask: was_run=true made the scheduler-throw catch satisfy the promise inline and then let raw member ~std::function free the callback, so that path did not share the release-then-satisfy cleanup run() and the queued-drop case use. Record the scheduler exception in a new schedule_error field and leave was_run false, so ~FinalTaskState releases the callback (under SCOPE_EXIT_SAFE) before satisfying the promise, and surfaces the real CANNOT_SCHEDULE_TASK. All three not-run paths now share one cleanup invariant. gtest: assert waitAll() surfaces the scheduler's own exception (code and message), not a synthesized one, pinning the schedule_error bridge. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Head branch was pushed to by a user without write access
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260702-225100 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 139/147 (94.56%) · Uncovered code |
CI finish ledger — ef5ba23CI fully finished (Finish Workflow + Mergeable Check pass). All checks green; no failures to own.
Session id: cron:our-pr-ci-monitor:20260703-053000 |
…read that owns a group threadPoolCallbackRunnerUnsafe wraps each callback in a CallbackRunnerTask shared_ptr and schedules it via ThreadPool::scheduleOrThrowOnError. When that schedule call throws (e.g. the pool is shutting down, or the thread fuzzer injects cannot_allocate_thread under stress), the not-yet-queued task shared_ptr is destroyed during stack unwinding, synchronously on the scheduling thread. ~CallbackRunnerTask() runs a ThreadGroupSwitcher to release the callback's captures under the task's thread group. It was constructed with the default allow_existing_group=false, whose contract is "assert the thread is not already attached to a different group" -- correct for run() (which only ever executes on a clean pool worker) but wrong for the destructor, which can also run on the scheduling thread. That thread is a normal query thread and already owns its own thread group, so the switcher threw LOGICAL_ERROR "Thread is already attached to a group (master_thread_id N)". A LOGICAL_ERROR is reported via abortOnFailedAssertion in debug/sanitizer builds, so the server aborted. Observed in Stress test (arm_tsan), STID 4298-42a4: DROP TABLE -> StorageMergeTree::shutdown -> MergeTreeDeduplicationLog::shutdown -> WriteBufferFromS3::makeSinglepartUpload -> TaskTracker::add -> scheduleOrThrowOnError throws -> ~CallbackRunnerTask on the DROP query thread. Pass allow_existing_group=true in the destructor: it remembers the scheduling thread's group and restores it after releasing the callback. No behavior change on the pool drain thread (a null/identical group is still a no-op). run() keeps allow_existing_group=false unchanged. This is distinct from and complementary to ClickHouse#108730 (which fixes a broken-promise future_error in TaskTracker::addFinal, a higher layer in ThreadPoolTaskTracker.cpp); this abort is one layer deeper, in the generic CallbackRunnerTask destructor shared by every threadPoolCallbackRunnerUnsafe caller, and would still fire with ClickHouse#108730 merged. A unit test in gtest_thread_group_switcher.cpp destroys an unrun CallbackRunnerTask on a thread that already owns a group and asserts the process does not abort and the thread's original group is restored. It aborts without this fix and passes with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…read that owns a group threadPoolCallbackRunnerUnsafe wraps each callback in a CallbackRunnerTask shared_ptr and schedules it via ThreadPool::scheduleOrThrowOnError. When that schedule call throws (e.g. the pool is shutting down, or the thread fuzzer injects cannot_allocate_thread under stress), the not-yet-queued task shared_ptr is destroyed during stack unwinding, synchronously on the scheduling thread. ~CallbackRunnerTask() runs a ThreadGroupSwitcher to release the callback's captures under the task's thread group. It was constructed with the default allow_existing_group=false, whose contract is "assert the thread is not already attached to a different group" -- correct for run() (which only ever executes on a clean pool worker) but wrong for the destructor, which can also run on the scheduling thread. That thread is a normal query thread and already owns its own thread group, so the switcher threw LOGICAL_ERROR "Thread is already attached to a group (master_thread_id N)". A LOGICAL_ERROR is reported via abortOnFailedAssertion in debug/sanitizer builds, so the server aborted. Observed in Stress test (arm_tsan), STID 4298-42a4: DROP TABLE -> StorageMergeTree::shutdown -> MergeTreeDeduplicationLog::shutdown -> WriteBufferFromS3::makeSinglepartUpload -> TaskTracker::add -> scheduleOrThrowOnError throws -> ~CallbackRunnerTask on the DROP query thread. Pass allow_existing_group=true in the destructor: it remembers the scheduling thread's group and restores it after releasing the callback. No behavior change on the pool drain thread (a null/identical group is still a no-op). run() keeps allow_existing_group=false unchanged. This is distinct from and complementary to ClickHouse#108730 (which fixes a broken-promise future_error in TaskTracker::addFinal, a higher layer in ThreadPoolTaskTracker.cpp); this abort is one layer deeper, in the generic CallbackRunnerTask destructor shared by every threadPoolCallbackRunnerUnsafe caller, and would still fire with ClickHouse#108730 merged. A unit test in gtest_thread_group_switcher.cpp destroys an unrun CallbackRunnerTask on a thread that already owns a group and asserts the process does not abort and the thread's original group is restored. It aborts without this fix and passes with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
@alexey-milovidov could you apply Three hits since this merged, all on 26.6, including the release branch itself: the 26.6 branch on 2026-07-17 (26.6.2.73, 26.6 is the only affected branch. 26.7 already has the fix by ancestry, and 26.5, 26.3 and 25.8 predate #105487, so they have no The port onto 26.6 has one conflict, the |
…racker-broken-promise-on-schedule-failure Fix broken_promise abort when scheduling the final S3 multipart upload task fails
Related: #105487
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
std::future_error("The associated promise has been destructed prior to the associated state becoming ready") that could surface, and abort the server in debug/sanitizer builds, when scheduling the final asynchronous S3/Azure multipart-upload completion task failed (for example under thread-pool exhaustion). The real scheduling error is now reported instead.Description
Found by the Stress test (
std::future_errorcode 1001, STID 2508-3dee and siblings); a chronic stress-test finding across all sanitizers, not tied to any single PR.TaskTracker::addFinalstores the future of the final task (the asyncWriteBufferFromS3::completeMultipartUpload) infuturesbefore the task is scheduled. When the scheduler (threadPoolCallbackRunnerUnsafe->ThreadPool::scheduleOrThrowOnError) threwCANNOT_SCHEDULE_TASK-- which the stress test injects via the thread fuzzer (cannot_allocate_thread_fault_injection_probability), and which can also happen on a genuinely exhausted pool -- the finalstd::packaged_taskwas destroyed without ever running. That left the already-stored future with a broken promise, sowaitAll()threwstd::future_error(astd::logic_error) instead of the realCANNOT_SCHEDULE_TASK.getCurrentExceptionMessageAndPatternaborts on anystd::logic_errorin debug/sanitizer builds, so a background merge that wrote its result to S3 aborted the server (in release builds the merge would instead fail with a misleading error).Both schedule sites (the
SCOPE_EXITinadd()and the run-now branch inaddFinal()) now go through a newscheduleFinalTask()helper, which runs the final task inline if scheduling fails. The task's future then always carries a proper result or the actual exception, sowaitAll()surfaces the real error and the background merge is retried normally instead of aborting.Reproduced locally with a MinIO-backed S3 disk and
cannot_allocate_thread_fault_injection_probabilityenabled: concurrent INSERT + OPTIMIZE FINAL on an S3-backed table aborted the server with thebroken_promisefuture_errorbefore the fix, and runs to completion (failures surface as the normal retryableCANNOT_SCHEDULE_TASK) after it. A unit test ingtest_thread_pool_task_tracker.cppexercises both schedule sites with a scheduler that throws on the final-task schedule and asserts nofuture_erroris produced (it fails withfuture_errorwithout the fix).CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108560&sha=fe57785cb9a849029b54f7f5334da0911d7877a5&name_0=PR&name_1=Stress%20test%20%28arm_debug%29
Version info
26.7.1.489(included in26.7and later)