Repository navigation
Fix server hang multiplying a quantile/median aggregate state by a huge constant - #110779
Conversation
…ge constant executeAggregateMultiply self-merges the ReservoirSampler-backed state with exponentiation by squaring, doubling ReservoirSampler::total_values (size_t) each step. A huge multiplier overflowed the total_values += b.total_values sum in ReservoirSampler::merge; the wrapped-small value made frequency = total_values / b.total_values drop below 1, turning for (double i = 0; i < sample_count; i += frequency) into a near-infinite loop with no cancellation check. The query stayed running for ~3100s after being cancelled, tripping the stress-test hung check. Saturate the sum on overflow so frequency stays >= 1 and the loop is bounded. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260717-042200 |
|
cc @alexey-milovidov @rschu1ze — could you review? Bounds a |
|
Workflow [PR], commit [e73009d] Summary: ✅
AI ReviewSummaryThis PR fixes the Final VerdictStatus: ✅ Approve |
CI finish ledger — 4a5fb73Every failure below has an owner (a fixing PR). No failure is caused by this PR's diff (a 7-line
Neither touches |
A saturated (SIZE_MAX) state can still be merged into another state via the insert() branches of merge() (b.total_values <= sample_count, or the current state's total_values <= sample_count). The plain ++total_values in insert() wrapped SIZE_MAX to 0 and then reached genRandom(0) = UB / a debug assert. Guard the increment to keep total_values saturated once it hits the max, and extend the regression test to cover huge_state + small_state in both operand orders. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Good catch, fixed in e73009d. insert() now guards the increment so total_values stays saturated at SIZE_MAX instead of wrapping to 0. Confirmed on a debug build that both merge orders aborted at genRandom(0) before the change and pass after: SELECT length(toString(finalizeAggregation((18446744073709551615 * (SELECT medianState(number) FROM numbers(100000))) + (SELECT medianState(number) FROM numbers(1))))); Both are added to 04603 in both operand orders. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 8/9 (88.89%) · Uncovered code |
CI finish ledger - e73009dCI fully finished on this head (Config Workflow, Finish Workflow, Mergeable Check, CH Inc sync all green). No failed checks: zero FAIL/ERROR test rows across all jobs. The bot-CR fix (saturate Session id: cron:our-pr-ci-monitor:20260717-193000 |
…voir-sampler-multiply-total-values-overflow Fix server hang multiplying a quantile/median aggregate state by a huge constant
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server hang when multiplying a
median/quantile(ReservoirSampler-backed) aggregate function state by a huge integer constant (e.g.medianState(x) * 18446744073709551615). The query became unresponsive to cancellation and could run indefinitely.Description
Found by the server-side AST fuzzer in
Stress test (arm_asan_ubsan, s3), auto-matched to the hung-check umbrella #107941. Investigated per @alexey-milovidov request on #110606.The query still running at hung-check time was
SELECT finalizeAggregation(10 * (SELECT medianState(number) FROM numbers(100000))),is_cancelled=1but elapsed ~3100s. Stack:ReservoirSampler::merge->executeAggregateMultiply->QueryAnalyzer::resolveFunction(constant folding). (ThedictGetline in the report's error-frequency table was an unrelatedILLEGAL_TYPE_OF_ARGUMENTexception, not the hang.)Root cause:
executeAggregateMultiplyself-merges the reservoir with exponentiation by squaring, doublingReservoirSampler::total_values(size_t) each step. A huge multiplier overflows thetotal_values += b.total_valuessum inReservoirSampler::merge; the wrapped-small value makesfrequency = total_values / b.total_valuesdrop below 1, sofor (double i = 0; i < sample_count; i += frequency)runs billions of iterations with no cancellation check.Fix: saturate the sum on overflow so
frequencystays >= 1 and the loop is bounded.ReservoirSamplerDeterministic::mergeis not affected (it merges viainsertImpl, no frequency loop).Reproduced deterministically (fails/hangs on master, passes with the fix); regression test
04603_reservoir_sampler_multiply_total_values_overflowadded.Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110606&sha=93359c59c1f878daeaedc91e6383f9e4ae893ce3&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29
Version info
26.7.1.1123(included in26.7and later)