Skip to content

Make h3kRing and its siblings observe cancellation while expanding a block - #115893

Merged
alexey-milovidov merged 1 commit into
masterfrom
h3-expansion-observes-cancellation
Aug 22, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
h3-expansion-observes-cancellation

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

Related: #112203
Related: #113924
Related: #112273
Related: #115773

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed h3kRing, h3HexRing, h3Line and h3ToChildren ignoring max_execution_time and KILL QUERY while expanding a block of rows.

Description

Stress test (amd_msan) on master failed the hung check with a single query that had been
cancelled 993 s earlier, against its own max_execution_time = 10:

SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023)))) FROM numbers(150000)

report,
is_cancelled: 1, peak_memory_usage: 127 MB, the working thread inside FunctionH3KRing::executeImpl.
The arm_asan_ubsan master run 1 h earlier hung on the same shape.

These four functions expand every row of a block inside one IFunction::executeImpl call, and the
executor evaluates max_execution_time and KILL QUERY only between blocks, so nothing observes the
query's QueryStatus until the whole block is done. The per-row caps these functions already have
bound one row, not the sum over the block, and the size of a row's result is driven by the arguments
rather than by the number of rows read.

h3kRing is the worst of the four, and it is the reason this shows up as a hang rather than as an
error. A row costs maxGridDiskSize(k) regardless of how many cells exist at the cell's resolution,
and on a coarse cell gridDiskDistancesUnsafe fails on the pentagons, after which h3 clears that
whole buffer and walks the grid recursively. 579205133326352383 is a resolution 0 cell, where the
whole grid is 122 cells, so at k = 1023 each row allocates and zeroes 25 MB to produce 122 cells:
the query never comes near a memory limit, it just runs. The other three produce what they cost, so
they are additionally bounded by max_memory_usage.

Measured on a release build, the query the fuzzer produced, one block:

SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023))))
FROM numbers(65409)
SETTINGS max_block_size = 65409, max_execution_time = 1
elapsed
before 250088 ms
after 1004 ms

Implementation

The checkpoint uses makeCancellationCheck and CancellationBudget, which are already on master and
used by countSubstrings, replaceString, countMatches and isProbablePrime, so this adds no new
constant and no new mechanism. Work is charged in the bytes a row expands into rather than in rows, so
the check fires once per row while k is large and about every sixteen thousand rows while it is 1.

h3Line charges in both of its loops: gridPathCellsSize walks the grid rather than being arithmetic,
so its sizing pass is expensive on its own. h3HexRing's sizing loop is 6 * k arithmetic plus a cell
validation and is left alone.

This is a narrower version of #113924, which was closed. The objection there was the four hand-written
items_between_cancellation_checks = 10'000 constants; the shared helper that removes them landed on
master in #113369 and has been used by merged work since (#114032). If the preferred answer is still a
single generic check before IFunction::executeImpl, note that it cannot bound these four, because the
whole block is inside one such call.

Testing

05029_h3_kring_cancellation.sh runs the reported shape under max_execution_time = 1 inside an outer
timeout 60, so the oracle is the outer timeout rather than a measured elapsed time, and asserts the
results are unchanged. It takes 1.7 s fixed and cannot finish unfixed. All 59 existing h3 tests pass
against the patched binary with no hung queries.


Workflow [PR]
Sync PR [sync-upstream/pr/115893]

Version info

  • Merged into: 26.8.1.1908 (included in 26.8 and later)

…block

`h3kRing`, `h3HexRing`, `h3Line` and `h3ToChildren` expand every row of a block
inside one `IFunction::executeImpl` call, and the executor evaluates
`max_execution_time` and `KILL QUERY` only between blocks, so nothing observed
the query's `QueryStatus` until the whole block was done. The size of a row's
result is driven by the arguments rather than by the number of rows read, so
the per-row caps these functions already have bound one row, not the block.

`h3kRing` is the worst of the four. A row costs `maxGridDiskSize(k)` regardless
of how many cells exist at the cell's resolution, and on a coarse cell
`gridDiskDistancesUnsafe` fails on the pentagons, after which h3 clears that
whole buffer and walks the grid recursively. So `h3kRing` on a resolution 0
cell with `k = 1023` allocates and zeroes 25 MB per row to produce 122 cells:
it never comes near a memory limit, it just runs. The other three produce what
they cost, so they are additionally bounded by `max_memory_usage`.

Measured on a release build:

    SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023))))
    FROM numbers(65409)
    SETTINGS max_block_size = 65409, max_execution_time = 1

    before: 250088 ms      after: 1004 ms

The checkpoint uses `makeCancellationCheck` and `CancellationBudget`, which are
already on master and used by `countSubstrings`, `replaceString`, `countMatches`
and `isProbablePrime`, so this adds no new constant. Work is charged in the
bytes a row expands into: the check then fires once per row while `k` is large
and about every sixteen thousand rows while it is 1. `h3Line` charges in both of
its loops because `gridPathCellsSize` walks the grid rather than being
arithmetic; `h3HexRing`'s sizing loop is `6 * k` arithmetic and is left alone.

Reported by Stress test (amd_msan) on master, where this query was still running
993 s after cancellation against its 10 s limit:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d3d7dc9bb6b2dfe932910da82715bcbe7d0d6806&name_0=MasterCI&name_1=Stress%20test%20%28amd_msan%29

Related: #112203
@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [19bac1b]

Summary: ✅


AI Review

Summary

This PR adds cancellation checkpoints to h3kRing, h3HexRing, h3Line, and h3ToChildren, plus a new regression test for the h3kRing timeout repro. The overall approach is sound, but h3Line still leaves one expensive sizing walk outside any pre-call checkpoint, so the advertised fix is incomplete for that function.

Findings

⚠️ Majors

  • [src/Functions/h3Line.cpp:127] h3Line still starts gridPathCellsSize without first observing query cancellation. If a query is already timed out or killed when a new row begins, the function will still do one full sizing walk before budget.charge(...) runs, which leaves the sizing pass unbounded for that row. Add a pre-call cancellation checkpoint before gridPathCellsSize, then keep the existing size-based charge after it.
Final Verdict

Changes requested: h3Line still misses cancellation before the expensive gridPathCellsSize pass, so the PR does not fully deliver the behavior it claims for that function.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.40% +0.10%

Changed lines: Changed C/C++ lines covered: 46/51 (90.20%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 22, 2026
Comment thread src/Functions/h3Line.cpp
"Line cannot be computed between start H3 index {} and end H3 index {}, error: {}",
start, end, err);

budget.charge(static_cast<size_t>(size) * sizeof(H3Index));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gridPathCellsSize is still entered without any pre-call checkpoint. If the query is killed or times out after the previous row, this loop will still do one full path walk before budget.charge(...) runs, so h3Line can still ignore cancellation for the entire sizing pass of that row.

The other three functions charge before their expensive H3 call for exactly this reason. Here I think you need a direct check_cancellation() (or equivalent pre-call checkpoint) before gridPathCellsSize, and then keep the size-based budget.charge(...) after it so later checks still scale with the line length.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will still do one full sizing walk before budget.charge(...) runs

N.p.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 19bac1b91 with master f4b987c4c (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 705.30 MiB 702.28 MiB -3.02 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

10 translation units recompiled, 14 s compile time in total, 10 of them have a recent master baseline.

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 22, 2026
@alexey-milovidov alexey-milovidov self-assigned this Aug 22, 2026
Merged via the queue into master with commit 3fbe9ad Aug 22, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the h3-expansion-observes-cancellation branch August 22, 2026 18:35
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 22, 2026
@clickgapai

Copy link
Copy Markdown
Contributor
Reproducer
-- max_execution_time is ignored: ~250 s on 26.7 release, ~1 s once fixed
SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023))))
FROM numbers(65409)
SETTINGS max_block_size = 65409, max_execution_time = 1, max_memory_usage = 0;

▶ Run on ClickHouse Fiddle

Results:

  • Reproduces on: 26.7, 26.6, 26.5, 26.3, 25.8
  • Does not reproduce on: master
  • Tested versions: master, 26.7, 26.6, 26.5, 26.3, 25.8
  • Also tested (unsupported, context only): 26.2 reproduces
  • ✓ Fix is on master — this PR's change fixes the bug on master. The releases listed above still have the bug and need the backport.

Backport the fix to 26.7, 26.6, 26.5, 26.3, 25.8.

CC component owner: @alexey-milovidov @scanhex12

Analysis metadata

comp-geo · Severity P2 · Finding phase_d_pr115893

  • Verified against master build 1562fec97aea (aarch64)
Introducing change: not identified
  • No clean release baseline found and no lower bound established — source-build bisect is required to pin the introducing PR (not yet performed).
  • Walked 6 release(s) below the supported window: still reproduces down to 25.2.
  • Secondary components: comp-testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants