Repository navigation
Never cancel a query before its max_execution_time has elapsed - #114559
Conversation
`CancellationChecker::appendTask` derived the deadline from the current time
truncated to whole milliseconds, and then aligned it up to the 100 ms grid the
worker batches deadlines on. The alignment normally hides the truncation, but
when the deadline already sits on the grid - one task in a hundred - it adds no
padding, and the deadline lands up to 1 ms before `now + max_execution_time`.
The query is then cancelled ahead of its own timeout and fails with a
self-contradictory message:
Code: 159. DB::Exception: Timeout exceeded: elapsed 999.672 ms, maximum: 1000 ms.
Round the current time up to a whole millisecond instead. That is the direction
the code around it already promises: "Tasks may be cancelled slightly later than
their exact timeout, but never before".
This is what makes `02122_join_group_by_timeout` flaky: it asserts that a
`max_execution_time = 1` cancellation takes at least 1000 ms, which an early
cancellation breaks. In the server logs of the two `Fast test` runs below the
query was cancelled at 999.672 ms and at 999.630 ms. Only the `throw` overflow
mode is affected - in `break` mode the checker re-reads the elapsed time before
acting on the deadline, so an early wake-up does nothing.
The deadline computation moved to `CancellationChecker::taskDeadlineMs` so that
the invariant can be tested over every sub-millisecond phase against every phase
of the grid; the new unit test fails with the old truncation.
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=gh-readonly-queue/master/pr-113021-93e2851fddb5add7bad5b28bcef780be2f6ff7bb&sha=bef88e3d96ecb0f57a0f8d81f6b7ee2117db19fd&name_0=MergeQueueCI&name_1=Fast%20test
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=gh-readonly-queue/master/pr-110451-2f02abe52a79c02fb554728f14d8fa847dcbc1e6&sha=2eba84a8a12a21c36516158d4fbe9530d3f0af8c&name_0=MergeQueueCI&name_1=Fast%20test
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [2ce3abe] Summary: ✅
AI ReviewSummaryThis PR fixes premature Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 124/126 (98.41%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
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 units197 translation units recompiled, 1613 s compile time in total, 197 of them have a recent master baseline. |
|
🕵 That job takes the test files from the PR and compiles them against the pre-fix source, expecting the test to build and fail there. The new test calls Any unit-test bug fix that extracts a helper to make the buggy computation testable hits the same wall: the only way around it would be to assert the invariant through The rest of CI is green, and the AI review reports no findings. Merged |
|
🕵 Audited the remaining CI failure. |
`appendTask` clamped every tracked timeout to `MAX_TIMEOUT_US` (one year), while `QueryStatus::throwProperExceptionIfNeeded` and `ExecutionSpeedLimits::checkTimeLimit` report the full configured `max_execution_time`. With `timeout_overflow_mode = 'throw'` and a `max_execution_time` above one year, the query was therefore cancelled at exactly one year while the exception text claimed the configured, much larger limit - which is precisely the "cancelled before its own timeout" contract this pull request establishes. The clamp existed only because `std::condition_variable::wait_for` converts its duration to nanoseconds internally and overflows for far-future deadlines. That is a property of the wait, not of the timeout, so it is now handled where it belongs: the worker waits in slices of at most one day and re-arms afterwards, and `taskDeadlineMs` saturates the deadline (upwards, never earlier) only when `now + timeout` genuinely does not fit into `Int64` microseconds - about 292 thousand years away. Addresses the review finding on `src/Interpreters/CancellationChecker.cpp:107`.
Converting the timeout to nanoseconds for the comparison overflowed once `max_execution_time` exceeded `UInt64::max()` nanoseconds (about 584 years), which `SettingFieldSeconds` accepts: the first overflowing value wrapped to 384 ns, so `checkTimeLimit` started throwing `TIMEOUT_EXCEEDED` almost immediately instead of never. The elapsed time is now compared in whole microseconds instead, which can only fire the timeout up to a microsecond later - never before it. The same arithmetic is fixed in `KafkaSource::checkTimeLimit`, `NATSSource::checkTimeLimit`, and `StorageKafka2::pollConsumer`, and the new `ExecutionSpeedLimits.HugeMaxExecutionTimeDoesNotOverflow` gtest pins the exact rollover point. Co-Authored-By: Claude Fable 5 <[email protected]>
`taskDeadlineMs` truncated the current time to whole microseconds before adding the timeout. When the resulting deadline landed exactly on a whole millisecond and on a `CANCELLATION_GRID_MS` boundary, the alignment added no padding, so the returned deadline was up to 999 ns before `now + timeout` - the same "cancelled before its own timeout" class this pull request removes, one scale down. Round the current time *up* to a whole microsecond instead, so the deadline is never earlier than the exact one. The gtest sampled the sub-millisecond phase of `now` in steps of 9973 ns and so never reached the last microsecond of a millisecond, which is exactly where this happens. It now sweeps every microsecond phase together with the nanosecond remainders inside it. `InterpreterSystemQuery::syncMerges` still capped the deadline at `steady_clock::time_point::max()`, about 292 years on a nanosecond clock, while `max_execution_time` accepts far larger values - so `SYSTEM SYNC MERGES` could throw `TIMEOUT_EXCEEDED` centuries before the configured timeout. It now compares the elapsed time against the timeout, which needs no absolute deadline and therefore no cap.
…cellation Conflict in `InterpreterSystemQuery::syncMerges`: master started clamping the `SYSTEM SYNC MERGES` timeout with `saturatedMilliseconds`, which caps it at one year - the same silent truncation this pull request removes elsewhere. Kept this branch's elapsed-time comparison, which needs no absolute deadline and therefore no cap, and preserves the microsecond precision of `max_execution_time`.
|
🕵 Audited the remaining CI red after pushing the review fixes.
with @groeneai, could you investigate that hung check and provide a fix in a separate pull request? If a fix is already in progress, please link it here.
|
|
The fix is already merged: #115773 (merged 2026-08-21 21:05:09Z, Your read is right and I agree this pull request is not the cause. I filed that same row on 2026-08-20 from the same report; a 30-day census of hung-check rows carrying One correction to the mechanism, because it changed what the fix had to be: the query is not parked, it is spinning. The with the other three threads waiting on it, so there is no deadlock. That is also why Verified on master at The stress run is from 2026-08-20, before the merge, so it could not have carried the fix. Your base
|
CancellationChecker::appendTaskderived a query's cancellation deadline from the current time truncated to whole milliseconds, and then aligned it up to the 100 ms grid the worker batches deadlines on. The alignment normally hides the truncation, but when the deadline already sits on the grid - one query in a hundred - it adds no padding, and the deadline lands up to 1 ms beforenow + max_execution_time. The query is then cancelled ahead of its own timeout and fails with a self-contradictory message:The current time is now rounded up to a whole millisecond instead, which is the direction the code around it already promises: "Tasks may be cancelled slightly later than their exact timeout, but never before". Only the
throwoverflow mode was affected: inbreakmode the checker re-reads the elapsed time before acting on the deadline, so an early wake-up does nothing.This is what makes
02122_join_group_by_timeoutflaky - it asserts that amax_execution_time = 1cancellation takes at least 1000 ms, andquery_duration_msis measured from a stopwatch that starts before the deadline is computed. The failure shows up asquery_duration 1becomingquery_duration 0, always on one of the twothrow-mode queries and never on thebreak-mode ones. In the server logs of bothFast testruns below, the query was cancelled early - at 999.672 ms and at 999.630 ms of its 1000 ms timeout.The deadline computation moved into
CancellationChecker::taskDeadlineMsso that the invariant can be tested directly: the new unit test checks every sub-millisecond phase ofnowagainst every phase of the grid, for a range of timeouts, and fails with the old truncation.CI reports of the flaky test:
The truncation is as old as the checker itself. Before deadlines were aligned to a grid in #95563 the deadline was plainly
floor(now) + timeout, i.e. early for every query rather than for one in a hundred; and the assertion that catches it was only sharpened fromround(query_duration_ms / 1000) BETWEEN 1 AND 60toquery_duration_ms BETWEEN 1000 AND 60000in daf1b49, which is why the flake is recent.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A query could be cancelled up to 1 ms before its
max_execution_timehad passed, failing with a self-contradictoryTimeout exceeded: elapsed 999.672 ms, maximum: 1000 ms.Workflow [PR]
Sync PR [sync-upstream/pr/114559]
Version info
26.8.1.1911(included in26.8and later)