Skip to content

RFC: Add a server setting to allow any allocation to throw MEMORY_LIMIT_EXCEEDED - #105265

Merged
azat merged 2 commits into
ClickHouse:masterfrom
azat:memory-tracker-min-alloc-throw-size
May 27, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
azat:memory-tracker-min-alloc-throw-size

Conversation

@azat

@azat azat commented May 18, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Add a server setting (min_allocation_size_to_throw_on_memory_limit) to allow any allocation to throw MEMORY_LIMIT_EXCEEDED, this should help with preventing OOMs

Right now, any new cannot throw, this is due exception safety issues, and only explicitly marked can throw MEMORY_LIMIT_EXCEEDED.

But this can lead to real OOMs, i.e. when kernel will kill clickhouse-server, even though we are trying hard to track memory properly.

Add a server setting min_allocation_size_to_throw_on_memory_limit (UInt64 bytes, default 0). When non-zero, the throwing operator new variants are allowed to raise MEMORY_LIMIT_EXCEEDED. The assumption is that code with big enough allocations does not exception safety issues.

Note, places that allows (Allocator)/never allows (malloc) MEMORY_LIMIT_EXCEEDED before are not affected.

Version info

  • Merged into: 26.6.1.204

@azat azat changed the title RFC: Add a server setting to allow new() throw MEMORY_LIMIT_EXCEEDED RFC: Add a server setting to allow any allocation to throw MEMORY_LIMIT_EXCEEDED May 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [917c934]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
ThreadSanitizer: data race (STID: 3821-5185) FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL

AI Review

Summary

This PR introduces min_allocation_size_to_throw_on_memory_limit and rewires throwing operator new tracking to optionally enforce MEMORY_LIMIT_EXCEEDED by allocation size. The core contract is still violated in the current implementation: mixed untracked batches can apply the wrong enforcement mode at flush time, so large allocations may be accounted as non-throwing when the threshold is below max_untracked_memory.

Findings
  • ❌ Blockers
    • [src/Common/CurrentMemoryTracker.cpp:81] and [src/Common/CurrentMemoryTracker.cpp:88]: CurrentMemoryTracker::allocImpl batches current_thread->untracked_memory but flushes with only the latest enforce_memory_limit flag. This breaks the setting contract (min_allocation_size_to_throw_on_memory_limit) because a large allocation tracked with enforce_memory_limit=true can be flushed later under false after a smaller allocation crosses untracked_memory_limit.
    • Impact: the new setting is not reliable for thresholds below max_untracked_memory; allocations that should be eligible to throw can silently bypass enforcement.
    • Suggested fix: flush pending untracked bytes when enforcement mode changes (similar to blocker-level change handling), or keep separate untracked accumulators per enforcement mode and flush each with its own flag.
Tests
  • ⚠️ Add a focused regression that sets min_allocation_size_to_throw_on_memory_limit < max_untracked_memory and reproduces mixed-size allocation order (large then smaller) to prove the flush path preserves enforcement semantics.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
  1. Fix CurrentMemoryTracker::allocImpl so enforce_memory_limit semantics are preserved across untracked_memory batching.
  2. Add a regression test for the mixed-batch case (threshold < max_untracked_memory).

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 18, 2026
Comment thread src/Common/CurrentMemoryTracker.cpp Resolved
AllocationTrace CurrentMemoryTracker::allocThrow(Int64 size)
{
const bool enforce_memory_limit = static_cast<UInt64>(size) >= min_allocation_size_to_throw_on_memory_limit.load(std::memory_order_relaxed);
return allocImpl(size, enforce_memory_limit);

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.

allocThrow decides enforce_memory_limit per allocation size, but allocImpl batches untracked bytes and only applies that flag when the batch is flushed. This means a large allocation (>= threshold) can be accumulated and then flushed under enforce_memory_limit = false if the allocation that crosses untracked_memory_limit is small, so the large allocation is effectively treated as non-throwing.

Concrete example with threshold 1 MiB: allocate 1.5 MiB (enforce=true, no flush yet), then several 0.9 MiB allocations; when flush is triggered by a 0.9 MiB allocation, the whole batch is flushed with enforce=false.

Can we flush pending untracked memory when the enforcement mode changes (similar to how blocker-level changes are handled), so threshold semantics are preserved?

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 add a note that it is recommended to set value of new setting >= max_untracked_memory

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.

The documentation note does not fix the runtime contract here. CurrentMemoryTracker::allocImpl still batches current_thread->untracked_memory and applies enforce_memory_limit from only the allocation that crosses untracked_memory_limit at flush time (memory_tracker->allocImpl(current_untracked_memory, enforce_memory_limit)).

So when min_allocation_size_to_throw_on_memory_limit is below max_untracked_memory, a large allocation can still be accounted in a later flush with enforce_memory_limit = false.

Concrete trace (default max_untracked_memory = 4 MiB, threshold 2 MiB):

  1. allocate 3 MiB (enforce_memory_limit = true, no flush),
  2. allocate 1.5 MiB (enforce_memory_limit = false, flush happens),
  3. flush executes memory_tracker->allocImpl(4.5 MiB, false) and bypasses the intended size-gated enforcement.

Please flush pending untracked bytes when enforcement mode changes (or keep separate untracked buckets by mode), and add a regression test that uses a threshold below max_untracked_memory.

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.

I re-checked the current head, and this is still live in CurrentMemoryTracker::allocImpl: pending current_thread->untracked_memory is flushed with the latest enforce_memory_limit value only (memory_tracker->allocImpl(current_untracked_memory, enforce_memory_limit)), so mixed-size batches can bypass the configured threshold.

Concrete trace with default max_untracked_memory = 4 MiB, min_allocation_size_to_throw_on_memory_limit = 2 MiB:

  1. allocate 3 MiB via throwing operator new (enforce_memory_limit = true), no flush;
  2. allocate 1.5 MiB (enforce_memory_limit = false), now flush fires;
  3. flush applies allocImpl(4.5 MiB, false) and the 3 MiB chunk is effectively treated as non-throwing.

So the setting contract is still violated for thresholds below max_untracked_memory. This needs either a flush when enforcement mode changes, or separate untracked buckets per enforcement mode.

Add a server setting (`min_allocation_size_to_throw_on_memory_limit`) to
allow any allocation to throw `MEMORY_LIMIT_EXCEEDED`, this should help
with preventing OOMs

Right now, any `new` cannot throw, this is due exception safety issues,
and only explicitly marked can throw `MEMORY_LIMIT_EXCEEDED`.

But this can lead to real OOMs, i.e. when kernel will kill
clickhouse-server, even though we are trying hard to track memory
properly.

Add a server setting `min_allocation_size_to_throw_on_memory_limit`
(UInt64 bytes, default 0). When non-zero, the throwing `operator new`
variants are allowed to raise `MEMORY_LIMIT_EXCEEDED`. _The assumption
is that code with big enough allocations does not exception safety
issues._

Note, places that allows (Allocator)/never allows (malloc)
`MEMORY_LIMIT_EXCEEDED` before are not affected.
@azat
azat force-pushed the memory-tracker-min-alloc-throw-size branch from 04b23f7 to a85155d Compare May 18, 2026 19:14

@nickitat nickitat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since we assume it will only be used in non-default configurations (CI or debugging OOM kills), should we really bother with this minimal limit? It seems like in such circumstances the more often it could be triggered the better.

@nickitat nickitat self-assigned this May 20, 2026
@azat

azat commented May 21, 2026

Copy link
Copy Markdown
Member Author

I would like to have an option to increase it, since it is unclear which value is generally "safe" (only one thing is clear it should be bigger then max_untracked_memory to avoid mixing)

@clickhouse-gh

clickhouse-gh Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: 92.72% (140/151) | lost baseline coverage: 9 line(s) · Uncovered code

Full report · Diff report

Comment thread src/Common/CurrentMemoryTracker.cpp Resolved
@azat
azat enabled auto-merge May 27, 2026 18:30
@azat
azat added this pull request to the merge queue May 27, 2026
@azat

azat commented May 27, 2026

Copy link
Copy Markdown
Member Author

Merged via the queue into ClickHouse:master with commit cd00255 May 27, 2026
163 of 166 checks passed
@azat
azat deleted the memory-tracker-min-alloc-throw-size branch May 27, 2026 19:01
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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