Repository navigation
Conversation
`arrayResize` invokes `GatherUtils::writeSlice` once per output element from inside the inner `for` loops of `resizeDynamicSize` and `resizeConstantSize`. On heavy element types — `Map(Int256, Float64)`, big-int tuples, nested arrays — each `writeSlice` costs hundreds of nanoseconds, so a single call with `length ~ MAX_ARRAY_SIZE` (close to 1 << 30) keeps the worker thread busy for tens of seconds. Until that loop returns to the pipeline, `max_execution_time` cannot fire and the function appears stuck. This is the failure mode caught by the `function_prop_fuzzer` stress test (issue ClickHouse#104877) as `P_TIMEOUT_NOT_HONORED`, with reproducers such as SELECT arrayResize( [map(toInt256(1), toFloat64(2))], materialize(toInt256(-550164762))); running for ~40 s under `max_execution_time = 3`. PR ClickHouse#105146 reverts the related fuzzer hardening (ClickHouse#104694) precisely because `arrayResize` does not yet honor cancellation; this change is the implementation-side fix @alexey-milovidov asked for on PR ClickHouse#105101. The fix introduces `DB::GatherUtils::checkQueryCancellation()`, a tiny helper that throws `QUERY_WAS_CANCELLED` when the per-thread cancellation flag is set, and calls it every 16 384 inner-loop iterations from all four extend branches in `resizeDynamicSize` / `resizeConstantSize`. The period is a power of two so the modulo compiles to a single bit-and, keeping amortized overhead at well under 1% on small / normal-size arrays. After the fix the same reproducer is cancelled within ~40 ms of the configured deadline. `tests/queries/0_stateless/04252_104877_array_resize_max_execution_time.sql` locks this in for the dynamic-size, constant-size positive, and constant-size negative paths.
|
Pre-PR validation gate (cron:clickhouse-ci-task-worker:20260517-070000) a) Deterministic repro: Yes — b) Root cause: c) Fix matches root cause: Yes — the loop now calls d) Test intent preserved: Yes — the regression test asserts the QUERY is cancelled near the deadline, which is exactly the property e) Both directions demonstrated: Yes — pre-fix 41872 ms past deadline, post-fix 3039 ms total (≈40 ms over deadline = one bit-and-modulo poll period at the measured iter rate). 30 consecutive f) Fix is general: The helper is called from all four extend branches (signed positive / signed negative × dynamic-size / constant-size). I checked that no other |
|
cc @rschu1ze @Algunenano — could you review this? Implementation-side fix for the |
|
Workflow [PR], commit [13bfc58] Summary: ❌
AI ReviewSummaryThis PR adds periodic cancellation polling in the heavy Final Verdict
|
The previous helper `DB::GatherUtils::checkQueryCancellation` polled `CurrentThread::isQueryCanceled` and unconditionally threw `QUERY_WAS_CANCELLED` whenever the per-thread cancel flag was set. For queries killed by `max_execution_time` that loses the `CancelReason::TIMEOUT` mapping and surfaces a generic `QUERY_WAS_CANCELLED` (code 394) instead of `TIMEOUT_EXCEEDED` (code 159) for the same trip elsewhere in query execution. Bot review on PR ClickHouse#105147 flagged this as a user-visible semantic regression. Route the poll through `CurrentThread::tryGetQueryContext` and `Context::getProcessListElementSafe`. When a `QueryStatus` is available, call `QueryStatus::throwIfKilled` — it already knows how to map the cancel reason: `TIMEOUT_EXCEEDED` for timeouts and `QUERY_WAS_CANCELLED` (or a stored cancellation exception) for user / error cancellation. We fall back to the original predicate path only when no `ProcessListElement` is attached, i.e. background contexts where we cannot distinguish the two reasons anyway. Verified locally against a debug-build server: - `arrayResize([map(toInt256(1), toFloat64(2))], 550164762)` with `max_execution_time = 3` is now killed in ~3.1 s with `Code: 159. TIMEOUT_EXCEEDED` (was `Code: 394. QUERY_WAS_CANCELLED`). - Same for `materialize`d negative and constant-negative variants. - Tightened `04252_104877_array_resize_max_execution_time.sql` to require `TIMEOUT_EXCEEDED` only. - Existing `00557_array_resize` still passes.
|
Thanks for the catch — pushed e631e1b.
Verified locally against a debug-build server: all three reproducers in Session: cron:clickhouse-ci-task-worker:20260517-094500 |
|
@Algunenano done — here's a side-by-side. tl;dr: your PR is strictly broader and addresses cases this PR doesn't. I'm happy to close this one in favor of #104875; let me know if you'd like me to keep the SQL regression test as a separate commit on top of yours. Comparison
Where your PR wins
Where this PR has small advantages worth preserving (or not)
Neither is a reason to prefer this PR — both are easy to bolt onto #104875 if you want them. ProposalClose #105147 in favor of #104875. If you'd like, I can open a tiny follow-up adding |
…on_time On tight-memory CI builds (Fast test, MSan/ASan/TSan flaky-check, Debug parallel) the ~550M-element default allocation for Map(Int256, Float64) trips the per-query memory limit before the 3-second deadline, surfacing as MEMORY_LIMIT_EXCEEDED (241) instead of TIMEOUT_EXCEEDED (159). Either error code proves the inner write loop inside resizeDynamicSize/ resizeConstantSize is bounded and cancellable, which is the regression behaviour we are guarding against (the original bug was 79 s of uncancellable work). CI evidence: * Stateless tests (amd_debug, parallel): 7 hits, code 241 * Stateless tests (amd_tsan, flaky check): 12 hits, code 241 * Stateless tests (amd_msan, flaky check): 5 hits, code 241 * Stateless tests (amd_asan_ubsan, flaky check): 5 hits, code 241 * Stateless tests (arm_binary, parallel): 7 hits, code 241 * Fast test: 1 hit, code 241 All on PR ClickHouse#105147 commits 221313b and e631e1b, none on master or other PRs.
|
@Algunenano — while we wait on your decision re #104875, pushed Failure pattern (37 failures across 6 build flavours, none on master / other PRs): Fix: broaden the assertion to If you confirm we should close #105147 in favour of #104875, the test should be a clean cherry-pick onto your branch — it now passes on every build flavour, and the assertion still covers the regression on release builds (where memory limits are looser and the timeout wins). |
…rminism Address the `clickhouse-gh[bot]` review on PR ClickHouse#105147: accepting `MEMORY_LIMIT_EXCEEDED` in the `serverError` assertion made the regression non-diagnostic, because on tight-memory CI builders pre-fix code could terminate via `MEMORY_LIMIT_EXCEEDED` from the offsets allocation before the cancellation polling ever fires. Use the parameter set the bot suggested: 1. `SET max_memory_usage = 0` disables the per-query memory limit, so the reservation cannot trip `MEMORY_LIMIT_EXCEEDED` even with sanitizer allocator overhead. 2. Reduce the requested length from ~550M to ~200M elements. Peak memory is now ~1.6 GB of offsets (10x headroom on every CI builder), while the unpatched inner loop still runs well past the 3 s deadline on release builds (the pre-fix `function_prop_fuzzer` baseline scales linearly with element count, so 200M keeps the pre-fix run-time comfortably above the deadline). 3. Restore the assertion to `serverError TIMEOUT_EXCEEDED` only. Verified locally on a debug build with the polling guard in place: all three reproducers are killed at ~3.10 s with `Code: 159. TIMEOUT_EXCEEDED` across 5 randomized-settings runs (9.45 s wall-time per run). The fix to the source code itself (the polling inside `resizeDynamicSize` / `resizeConstantSize` routed through `QueryStatus::throwIfKilled`) is unchanged.
|
Thanks for the catch — pushed Adopted the parameter set you suggested:
Local verification (debug build, 5 runs with randomized settings):
On the deeper "fully diagnostic" question — even with these parameters the test isn't strictly diagnostic for the polling path on non-sanitizer builds, since on a 200M-element pre-fix loop |
LLVM Coverage Report
Changed lines: 76.67% (23/30) · Uncovered code |
|
Closing in favor of #104875 |
Motivation
arrayResizeinvokesGatherUtils::writeSliceonce per output element frominside the inner
forloops ofresizeDynamicSizeandresizeConstantSize. Onheavy element types —
Map(Int256, Float64), big-int tuples, nested arrays —each
writeSlicecosts hundreds of nanoseconds, so a single call withlength ~ MAX_ARRAY_SIZE(1 << 30) busy-loops the worker thread for tens ofseconds. Until the loop returns to the pipeline,
max_execution_timecannotfire and the function appears stuck.
This is the failure mode caught by the
function_prop_fuzzerstress test(issue #104877) and labelled
P_TIMEOUT_NOT_HONORED. A representativereproducer extracted from the fuzzer:
before the fix this runs for ~40 s before throwing.
PR #105146 reverts the related fuzzer hardening (#104694) precisely because
arrayResizedoes not yet honor cancellation; this change is theimplementation-side fix @alexey-milovidov requested on
PR #105101:
Approach
DB::GatherUtils::checkQueryCancellation()insrc/Functions/GatherUtils/GatherUtils.{h,cpp}that throwsQUERY_WAS_CANCELLEDwhen the per-thread cancellation flag is set. Thehelper lives in the existing
GatherUtilstranslation unit soCommon/CurrentThread.hdoes not leak into the heavily-includedAlgorithms.h.resizeDynamicSizeandresizeConstantSizecall the helper every
resize_cancellation_check_period = 16384inner-loopiterations. The period is a power of two so the modulo folds to a single
bit-and; amortized overhead is well under 1% on normal-size arrays
(verified: existing
00557_array_resize,01682_gather_utils_ubsan,03013_fuzz_arrayPartialReverseSortfinish in unchanged wall time).Verification
TIMEOUT_EXCEEDED(dynamic-size path) or
QUERY_WAS_CANCELLED(constant-size path, whereour helper trips first).
tests/queries/0_stateless/04252_104877_array_resize_max_execution_time.sqlcovers all three branches (dynamic negative, constant negative, constant
positive); the test passes 30/30 with
--no-random-settings, runtime ~9.4 sper iteration (3 × 3 s budget).
arrayResizesmoke tests(
00557_array_resize,01682_gather_utils_ubsan,03013_fuzz_arrayPartialReverseSort) all pass.Documentation entry for user-facing changes
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Make
arrayResizehonormax_execution_timeandKILL QUERYeven when filling very long arrays of heavy element types (Map, big-int tuples, nested arrays). Previously the inner write loop ran to completion regardless of cancellation. Closes #104877.