Skip to content

Make isProbablePrime cancellable and cap rounds to 256 - #104633

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
fix-is-probable-prime-cancellation
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
fix-is-probable-prime-cancellation

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

isProbablePrime on UInt128/UInt256 delegated to boost::multiprecision::miller_rabin_test, which has no cancellation hook. Under heavy sanitizer + ThreadFuzzer instrumentation, a single round can stretch into minutes; combined with the server-side AST fuzzer mutating the rounds literal, the function hangs the stress test's hung_check for the full 30-minute window.

This PR:

  • Adapts Boost's Miller-Rabin loop into Primality::detail::millerRabinTestBig and calls QueryStatus::checkTimeLimit between rounds, so both KILL QUERY and max_execution_time interrupt the function.
  • Rejects rounds > 256 with BAD_ARGUMENTS. 4^-256 < 10^-150 already, so larger values add no meaningful confidence and only widen the worst-case runtime under the AST fuzzer.

The hang was discovered by the AST-fuzzer-re-enablement PR #101354: every stress test variant hits a hung_check on the same root cause — isProbablePrime is is_cancelled=1 but stuck in boost::multiprecision::miller_rabin_test → eval_powm. Example: https://s3.amazonaws.com/clickhouse-test-reports/PRs/101354/f0038d26f27dc2d179136ae371e632746bab3c30/stress_test_arm_tsan/hung_check.log

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Function isProbablePrime now respects KILL QUERY and max_execution_time mid-computation, and rejects unreasonably large rounds arguments (limit: 256) with BAD_ARGUMENTS.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

`isProbablePrime` on `UInt128`/`UInt256` delegated to `boost::multiprecision::miller_rabin_test`, which has no
cancellation hook. Under heavy sanitizer + ThreadFuzzer instrumentation, a single round can stretch into
minutes; combined with the server-side AST fuzzer mutating the `rounds` literal, the function hangs the
stress test's `hung_check` for the full 30-minute window.

This commit:
- Adapts Boost's Miller-Rabin loop locally and calls `QueryStatus::checkTimeLimit` between rounds, so
  `KILL QUERY` and `max_execution_time` both interrupt the function.
- Rejects `rounds > 256` with `BAD_ARGUMENTS`. `4^-256 < 10^-150` already, so larger values add no
  meaningful confidence and only widen the worst-case runtime under the AST fuzzer.

See CI report on the AST-fuzzer-re-enablement PR:
#101354
https://s3.amazonaws.com/clickhouse-test-reports/PRs/101354/f0038d26f27dc2d179136ae371e632746bab3c30/stress_test_arm_tsan/hung_check.log

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [11694fc]

Summary: ✅


AI Review

Summary

This PR makes isProbablePrime cancellable for UInt128/UInt256 by replacing the uncancellable Boost call with an internal Miller–Rabin loop that checks query limits between rounds, and it adds an upper bound of 256 for rounds. The implementation is coherent with the stated motivation, includes updated source-level function documentation, and extends stateless tests for the new argument bound behavior.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 87.65% (71/81) · Uncovered code

Full report · Diff report

alexey-milovidov added a commit that referenced this pull request May 12, 2026
A single `Primality::isProbablePrime` call on a wide `UInt128`/`UInt256`
input runs many Miller-Rabin rounds; under the ASan+UBSan instrumentation
of `function_prop_fuzzer`, this can take longer than the test's 30 second
stop deadline, and the worker is then reported as stuck. The cancellation
hook added in #104633 cannot help here: the stress test has no
`QueryStatus`, so `check_cancelled` is a no-op.

Add a hard constraint that caps the first argument at 1000, forcing the
function through its fast bitmap / UInt64 path on every iteration. The
function is still exercised on all supported input types - just on small
values.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104639&sha=708387ba14ed6c25931b4d202c9198fc9d6cb223&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
PR: #104639

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@grantholly-clickhouse
grantholly-clickhouse self-requested a review May 12, 2026 18:30

@grantholly-clickhouse grantholly-clickhouse left a comment

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.

This should help CI run times.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Merged in #104639

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants