Repository navigation
Fix the global memory tracker going negative while max_server_memory_usage refuses an allocation - #131625
Conversation
…tracker `MemoryTracker::allocImpl` charges an allocation to the global `amount` and `rss` before deciding on it. Over the limit, the decision can take long: the jemalloc profile flush, the page and columns cache resize, and the overcommit wait (up to `memory_usage_overcommit_max_wait_microseconds`, 5 s by default). Meanwhile every `MemoryWorker` tick replaces `rss` with a measurement, and `amount` too with `memory_worker_correct_memory_tracker` (the default since ClickHouse#118094). A measurement cannot contain the pending charge, because the tracker is called before the allocation, so the tick erases it, and the refusal then subtracts the size from counters that no longer hold it. Both stay negative by the size of the refused allocation until the next tick, and meanwhile `max_server_memory_usage` admits allocations it should refuse. In the AST fuzzer the tracker read -500 GiB and the cgroup killed the server. `refusing_amount` (ClickHouse#127252) already counts these allocations for the peak. The correction now adds it to the measurement for `amount` and `rss`; the `MemoryTracking` metric and the peak keep the measurement. The allocation is registered before the profile flush, not after it, and released together with the rollback instead of at unwind. The registration and the releases use release order and the correction loads with acquire, so a correction that sees a registered size also sees that thread's charge, and one that sees a release also sees its rollback. A correction within a few instructions of the charge or the rollback can still miss or keep that allocation once, until the next correction. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (1 round on 7ca0a8c: 0 open findings)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261114-114102 |
|
Workflow [PR], commit [7ca0a8c] Summary: ❌
AI ReviewSummaryThe PR preserves pending refused allocations when the memory worker corrects the global counters. The refusal path is improved, but successful admission releases the pending charge before potentially blocking allocation bookkeeping finishes, leaving a path to the same undercount and memory-limit bypass. Findings❌ Blockers
Tests
Missing context / blind spots
Final Verdict❌ Block — Preserve the pending charge through successful allocation bookkeeping and cover the correction during peak reporting. |
| Int64 refusing_size = can_refuse ? size : 0; | ||
| if (refusing_size) | ||
| refusing_amount.fetch_add(refusing_size, std::memory_order_release); | ||
| SCOPE_EXIT({ |
There was a problem hiding this comment.
[Blocker] Keep an admitted charge registered through commitAllocation.
This guard runs when the over-limit block ends, before commitAllocation at line 601. If a cache resize admits the allocation, commitAllocation can still call updatePeak, which logs and can synchronously flush a jemalloc profile. The actual allocator has not run yet, but refusing_amount no longer contains its charge. A worker correction during that work therefore erases the charge again; a subsequent free or allocator-failure rollback subtracts bytes the counters no longer contain, recreating the negative tracker and memory-limit bypass.
For example, start with an 8 GiB tracker/peak and a 10 GiB limit: a 9 GiB request can be admitted after evicting 7 GiB of cache, leaving a 10 GiB charge and raising the peak. If a correction in that peak logging/profile flush measures 1 GiB, it replaces the charge with 1 GiB; releasing the 9 GiB request then leaves -8 GiB, ignoring small accounting noise. This window includes blocking work, beyond the short charge/rollback races disclosed in the description.
Keep the pending accounting alive through this work, retaining the explicit release after rejection rollback. Please add a regression that corrects the tracker from the successful path's peak logging/profile flush; the existing admission test corrects only inside autoResize, before this guard runs.
There was a problem hiding this comment.
Not changed: every successful allocation, in-limit or not, runs this bookkeeping with its charge unregistered, on master too (the updatePeak profile flush only when jemalloc_flush_profile_interval_bytes is set, 0 by default), and a correction there undercounts it until the next correction, while a refused allocation is rolled back right after a wait of up to 5 s, which is what took the tracker to -500 GiB in CI. Keeping every successful allocation registered until the allocator runs would add two atomic operations on a shared counter to each allocation, so I leave that out of this PR.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
Related: #127054
Related: #118094
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the global memory tracker going negative by the size of an allocation that
max_server_memory_usagerefused after waiting for memory, whenmemory_worker_correct_memory_trackeris enabled (the default since 26.10). While it was negative, the server memory limit admitted allocations it should have refused, and the server could be killed by the OOM killer.Description
On 26.10.2.103-stable, a query whose 4.47 GiB allocation is refused after a 2 s overcommit wait under a 2.79 GiB
max_server_memory_usageleaves the global memory tracker at -4.2 GiB (3 of 3 runs), and until the next memory worker tick the limit admits allocations past it.The tracker charges an allocation before deciding on it, and the decision can take seconds (overcommit wait, jemalloc profile flush, cache resize). Each memory worker tick replaces
rss, and with the correction on alsoamount, with a measurement, which cannot contain that charge, so the refusal subtracts the allocation from counters that no longer hold it.refusing_amount(#127252) already counts these allocations.updateAllocatedandupdateRSSnow add it to the measurement; the metric and the peak keep the measured value. It is registered before the profile flush and released with the rollback. No new member, no lock: this replaces #127054.CI: the tracker read -500.84 GiB, -63.14 GiB, and a 64 GiB allocation was admitted under a 40 GiB limit before the cgroup killed the server (#123890, #99639, #131551).
Validation: two gtests run a worker correction inside the over-limit decision, for a refused and an admitted allocation; on master both fail by exactly the allocation size. With the fix, the end-to-end repro shows no negative correction, also with the correction off.
A correction within a few instructions of the charge or the rollback can still miss or keep that allocation once, until the next correction.
Workflow [PR]
Sync PR [sync-upstream/pr/131625]