Repository navigation
Fix stack-use-after-scope in MergeTreeData::loadStatisticsOfPartsInParallel when scheduling a loader job throws - #127911
Conversation
…eduling throws `std::optional<ThreadPool> pool` is declared before the `load_parts` lambda (the lambda names `pool`), and the pool jobs captured `load_parts` by reference. When `scheduleOrThrowOnError` throws for a job after the first (thread creation failure, `CANNOT_SCHEDULE_TASK`), unwinding destroys `load_parts` before `~ThreadPool` joins the jobs that are still inside it, and they read the dead closure. Found by Stress test (arm_asan_ubsan) on master, STID 4822-4aba. The jobs now capture a copy of `load_parts`. Its members are references to objects declared before `pool` and to the caller's arguments, so nothing a job reads dies before the join. The gtest makes the first loaded part enable `CannotAllocateThreadFaultInjector` and keep its job running for 1 s, so the next scheduling throws while it runs. The sleep has to outlast the throw: a failed thread start builds an exception message with a symbolized stack trace, 0.1-0.5 s under ASan, so 50 ms let the job finish before the frame unwound. Under ASan it aborts in 20/20 runs without the fix and passes 200/200 with it. Closes ClickHouse#127895 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261027-030100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261027-013600 |
|
cc @alexey-milovidov @CurtizJ, could you review this and add the |
|
Workflow [PR], commit [c029c4c] Summary: ❌
AI ReviewSummaryThe PR fixes a stack-use-after-scope in Missing context / blind spots
Final VerdictStatus: ✅ Approve. No changes requested. |
CI finish ledger - c029c4cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first run of this head stopped at Session id: cron:our-pr-ci-monitor:20261027-203012 |
|
@groeneai, in the original PR, what did we miss in the code review or the original coding instructions to prevent this issue to happen, and how can we prevent it in general? Should we provide more specific instructions in our code review skill? Reflect on this and send a PR with general development process improvements. |
|
Coding. The bug came with the last review round of #126625, d05d94d. It moved the job body into the named closure Review. The Tests. Thread creation failures are injected only in Stress tests, with probability 0.0001 ( Known class. #95818 fixed the same bug class across 20 files (merged 2026-02-10) and added a warning with this exact shape to Prevention. Yes, the review skill should have a specific check. I'll send a PR with:
I'll post the link here. |
5332ba8
|
Sent #128365. One change from the list above: I dropped " |
Add three rules to the agent instructions: - `.claude/skills/review/SKILL.md`, triggered expansion "Jobs on other threads": list everything a scheduled job uses, including a lambda or `std::function` it calls, and check that each outlives the join on every exit after the first schedule (a later schedule throws, the scheduling thread throws while doing its own share, an early return). The joiner is declared after everything the jobs use, and a closure that refers to the pool is captured by value. - Same file, "A fix is new code": a re-review covers the code changed since the previous review, including the fixes for earlier findings, with the same gates as the first review. - `.claude/CLAUDE.md`: the same lifetime rule for authors. The last round of ClickHouse#126625 moved the job body of `loadStatisticsOfPartsInParallel` into a closure declared after the pool, called by the jobs through `[&]`. A later schedule that threw destroyed the closure before the pool joined the jobs (fixed by ClickHouse#127911). The AI review approved that round with "the prior findings are addressed", and Stress tests, the only ones that inject thread creation failures broadly, were green on the final head. Asked for in ClickHouse#127911 (comment) The reply there listed "`wait` rethrows" as an exit; it is not in the rule because `ThreadPool::wait` and `waitForAllToFinishAndRethrowFirstError` join all jobs before rethrowing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #127895
Related: #126625
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
loadStatisticsOfPartsInParallel(added in #126625) declaresstd::optional<ThreadPool> poolbefore theload_partslambda, and the pool jobs capturedload_partsby reference. WhenscheduleOrThrowOnErrorthrows for a later job (thread creation failure,CANNOT_SCHEDULE_TASK), unwinding destroysload_partsbefore~ThreadPooljoins the jobs that are still running it, so they read a dead stack object.Found by
Stress test (arm_asan_ubsan)on master: report (READ of size 8in the loop condition ofload_parts, object'load_parts' (line 1276)).The jobs now capture a copy of
load_parts. The closure holds only references topool, to objects declared before it and to the caller's arguments, so nothing a job reads is destroyed before the join. Behaviour is otherwise unchanged.The new gtest
MergeTreeData.LoadStatisticsOfPartsInParallelWhenSchedulingThrowsenablesCannotAllocateThreadFaultInjectorfrom the first loaded part and keeps that job running for 1 s, so the next scheduling throws while it runs. The sleep must outlast the throw itself: the exception message of a failed thread start carries a symbolized stack trace, which takes 0.1-0.5 s under ASan. Under ASan the test aborts with the same report as CI in 20/20 separate runs without the fix and passes 200/200 with it;05407_statistics_parallel_loadpasses 50/50 with random settings.Not in any release: #126625 is not in 26.10.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127911]
Version info
26.11.1.962-master(included in26.11and later)