Repository navigation
Conversation
A quota with a zero-length interval (CREATE QUOTA ... FOR INTERVAL 0 SECOND) made the server divide by the interval duration when the quota was consumed, crashing with SIGFPE (integer divide by zero) in EnabledQuota::Interval::getEndOfInterval. Reject a non-positive interval duration at quota creation instead. Both the SQL DDL path and the on-disk reload path go through updateQuotaFromQueryImpl; the XML config path already skips non-positive durations. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-5:20260714-075100 |
|
Workflow [PR], commit [c94bb7a] Summary: ❌
AI ReviewSummaryThis PR fixes the main crash path and closes most of the follow-up regressions around loading legacy invalid quotas, but one user-facing Findings
Tests
Final Verdict
|
The reject-non-positive-interval validation added in the previous commit runs through updateQuotaFromQueryImpl, which is shared by the CREATE/ALTER DDL path and the on-disk deserialization path (AccessEntityIO). After an upgrade a legacy FOR INTERVAL 0 SECOND quota still lives in the access storage, and QuotaCache::ensureAllQuotasRead eagerly reads every quota id at startup, so the parse exception would propagate out of DiskAccessStorage::readImpl and break access-storage load. Scope the validation to the user-facing DDL path (validate=true) and skip it on deserialization (validate=false). To keep a loaded legacy zero-interval quota from dividing by zero on consumption, guard getEndOfInterval to treat a non-positive duration as non-expiring, and skip the randomize path for it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Head branch was pushed to by a user without write access
|
Good catch. Fixed in e828767. The reject validation went through updateQuotaFromQueryImpl, which is shared by the CREATE/ALTER DDL path and the on-disk deserialization path (AccessEntityIO). I added a validate flag: true on the user-facing DDL path, false on deserialization, so a legacy FOR INTERVAL 0 SECOND quota still loads on startup after upgrade and DiskAccessStorage::readImpl no longer propagates a parse exception. To keep a loaded legacy zero-interval quota from dividing by zero on consumption, EnabledQuota::Interval::getEndOfInterval now treats a non-positive duration as non-expiring (returns the loaded end, never resets counters), and the Interval constructor skips the randomize path for it. Verified on a debug build (build id ba3505c) via a real DiskAccessStorage restart: a quota edited on disk to FOR INTERVAL 0 SECOND now loads on startup, appears in system.quota_usage, and consuming it keeps the server alive (no SIGFPE); CREATE QUOTA FOR INTERVAL 0 SECOND is still rejected with BAD_ARGUMENTS. |
Making a legacy on-disk zero-interval quota non-expiring kept one per_hash_used entry forever for every normalized query shape, trading the divide-by-zero for unbounded memory growth. Instead skip non-positive intervals in QuotaCache::rebuildIntervals so they are never enforced (no division, no per-hash bookkeeping), and drop the non-expiring branch in EnabledQuota. The create-time rejection is unchanged; deserialization still loads the entity so startup does not fail after upgrade. The regression test now assigns the quota to a dedicated user instead of default so it is parallel-safe. Co-Authored-By: Claude Opus 4.8 <[email protected]>
A legacy on-disk quota whose every interval has a non-positive duration is skipped entirely at consumption setup, so it no longer caches an empty Intervals per quota key nor a per-hash resolver entry, and the skip warning is emitted once at load time instead of once per key. The regression test now runs the positive-path query as the dedicated user so it actually consumes the quota. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The prior rework skipped a quota in chooseQuotaToConsumeFor whenever it had no enforceable interval, which also skipped a quota that simply has no limits at all (a normal track-nothing quota). That made system.quota_usage return no row for it and broke test_quota (test_simpliest_quota, test_add_remove_interval). Only a quota that had limits but every one is non-positive (a legacy on-disk zero-interval quota) is inert now; a quota with no limits is still set up and reports an empty usage row. Co-Authored-By: Claude Opus 4.8 <[email protected]>
When a legacy FOR INTERVAL 0 SECOND quota becomes inert on a warm/rolling upgrade path (SYSTEM RELOAD USERS or an older node sending a non-positive interval), setQuota kept its previously cached per-key Intervals in key_to_intervals. getAllQuotasUsage still surfaced those as stale NULL rows in system.quotas_usage. Clear key_to_intervals when the quota flips inert so no stale usage rows remain; nothing repopulates it while inert because chooseQuotaToConsumeFor skips the quota before getOrBuildIntervals. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The NORMALIZED_QUERY_HASH interval_resolver lambda did not check is_inert. A late in-flight query holding an old SingleQuota could miss the resolved intervals cache and call getOrBuildIntervals for a new hash, which re-inserts an empty Intervals into key_to_intervals and resurfaces stale NULL rows in system.quotas_usage after the quota became inert. Return nullptr when the owning quota is inert so nothing repopulates key_to_intervals while inert. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| QuotaPtr quota; | ||
| UUID quota_id; | ||
| const RolesOrUsersSet * roles = nullptr; | ||
| /// True when the quota had at least one limit but every limit has a non-positive interval |
Co-Authored-By: Claude Opus 4.8 <[email protected]>
An ALTER cloned from a legacy inert quota still carried its non-positive interval and wrote it back unchanged, so a user-facing ALTER/RENAME could republish the crash-triggering definition on a mixed-version cluster. Drop any non-positive interval from the final all_limits when validate is set. Add an integration test that seeds a persisted FOR INTERVAL 0 SECOND quota, reloads, and asserts it loads inert (server up, no quotas_usage rows) and that a later ALTER does not republish the zero interval. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 64/71 (90.14%) · Uncovered code |
A persisted FOR INTERVAL -1 SECOND quota reached the deserialize path as duration = -1 and then wrapped to 4294967295 when system.quotas and system.quota_limits cast the duration to UInt32. Keep only the genuine legacy zero-interval carve-out inert on load; drop negatives. Co-Authored-By: Claude Opus 4.8 <[email protected]>
A persisted FOR INTERVAL -1 SECOND quota was erased to empty all_limits on the deserialize path, so QuotaCache::setQuota left is_inert=false and the consumption path published an empty quota key, regrowing stale NULL rows in system.quotas_usage (per key, including per normalized_query_hash). Normalize negative durations to 0 on load instead of dropping them so they share the existing inert zero carve-out; the quota stays inert with no usage rows and system.quotas / system.quota_limits report 0 rather than the UInt32-wrapped 4294967295. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
| auto duration = query_limits.duration; | ||
|
|
||
| /// Reject only on CREATE/ALTER; deserialization (validate=false) still loads legacy quotas. | ||
| if (validate && !query_limits.drop && duration.count() <= 0) |
There was a problem hiding this comment.
Because this guard skips query_limits.drop, a legacy inert quota can still lose its sentinel through a user-facing NO LIMITS clause. Repro: load ATTACH QUOTA q KEYED BY normalized_query_hash FOR INTERVAL 0 SECOND MAX queries = 1000 TO ALL, then run ALTER QUOTA q FOR INTERVAL 0 SECOND NO LIMITS.
That hits the erase branch below, leaves all_limits.empty(), and QuotaCache::setQuota no longer marks the quota inert while chooseQuotaToConsumeFor reinstalls the normalized_query_hash resolver. The result is the same empty key_to_intervals / resolved_intervals_cache growth and stale system.quotas_usage rows that the earlier commits were fixing, just via an explicit NO LIMITS ALTER instead of TO / RENAME. We should either reject non-positive durations on NO LIMITS clauses too, or keep the 0 sentinel when dropping the last legacy-invalid interval without replacing it with a positive one.
CI finish ledger — c94bb7aCI fully finished on this HEAD (rework commit). Only
Note: the earlier Session id: cron:our-pr-ci-monitor:20260715-113000 |
|
I am going to ease this in another PR |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server crash (integer division by zero) when a quota with a zero-length interval (for example
CREATE QUOTA q FOR INTERVAL 0 SECOND MAX queries = 1000) was consumed. A non-positive quota interval duration is now rejected at quota creation.Description
Found by the fuzzer (STID 4141-5b13,
Stress test (arm_asan_ubsan, s3), PR #110228, sha d463af4): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110228&sha=d463af42c39ba64e89709493b3e9aa2f0393e22f&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29Root cause:
EnabledQuota::Interval::getEndOfIntervalcomputes(current_time - end + duration) / duration. With a zero-length intervalduration == 0, so consuming the quota divides by zero. In debug/UBSan builds this is reported as UB atlibcxx/include/__chrono/duration.h; in release builds it is a real SIGFPE (signal 8, integer divide by zero) that terminates the server.Repro:
Fix: reject a non-positive interval duration at quota creation in
updateQuotaFromQueryImpl, which is the single validation point used by both the SQL DDL path and the on-disk reload path (AccessEntityIO). The XML users-config path already skips non-positive durations. A fractional interval that rounds down to zero seconds (e.g.INTERVAL 0.4 SECOND) is rejected too, since it hits the same code path.Closes #110343