Skip to content

Reapply "Add UntrackedMemory asynchronous metric" - #106386

Merged
azat merged 9 commits into
masterfrom
untracked-memory-race
Jul 17, 2026
Merged

azat merged 9 commits into
masterfrom
untracked-memory-race

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Jun 3, 2026 •

Copy link
Copy Markdown
Member

Reapply #104690 after #106365.

Changelog category (leave one):

  • Improvement

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

Adds a new asynchronous metric UntrackedMemory (visible in system.asynchronous_metrics) that reports memory already allocated by threads but not yet accounted in the global memory tracking counter. Each thread accumulates small allocations locally and reports them in bulk. This helps explain discrepancies between MemoryTracking and the process's actual memory usage. MEMORY_LIMIT_EXCEEDED error messages now also include the amount of untracked memory, making it easier to understand why a query or the server hit its memory limit.

@mstetsyuk
mstetsyuk requested a review from azat June 3, 2026 13:48
@clickhouse-gh

clickhouse-gh Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [29839e5]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3) FAIL
Server died FAIL cidb
02122_parallel_formatting_TSVWithNames UNKNOWN cidb
03915_exchange_tables_race UNKNOWN cidb, issue
04062_analyzer_inline_view_type_conversion UNKNOWN cidb
00008_array_join UNKNOWN cidb
Unknown error FAIL cidb

AI Review

Summary

This PR reintroduces the UntrackedMemory asynchronous metric and threads its per-thread counter into MEMORY_LIMIT_EXCEEDED diagnostics by replacing ThreadStatus::untracked_memory with a registered counter. The overall direction makes sense, but there are still correctness gaps in the throwing path and in MemoryTrackerSwitcher, and the added tests still do not prove the behavior the PR claims.

Findings

⚠️ Majors

  • src/Common/CurrentMemoryTracker.cpp:103 After a failed flush, the code restores previous_untracked_memory but not its original untracked_memory_blocker_level. The exception path in MemoryTracker::allocImpl allocates under MemoryTrackerBlockerInThread(VariableContext::Global), so recursive allocations during Exception construction can leave the thread's blocker level at Global. The next flushUntrackedMemory then bypasses the query/user/process trackers and charges those restored bytes only to total_memory_tracker, which breaks memory accounting after MEMORY_LIMIT_EXCEEDED.
    Suggested fix: keep the saved blocker context alongside the restored buffer, and either flush the internal exception-construction bytes before restoring the caller's state or keep them in a separate buffer that still flushes under Global.

  • src/Common/CurrentMemoryTracker.cpp:91 The new diagnostic field still under-reports the current thread's buffered bytes on the path that throws MEMORY_LIMIT_EXCEEDED. The code stores 0 into the registered counter before memory_tracker->allocImpl formats the exception, so UntrackedMemoryRegistry::sum omits previous_untracked_memory until the catch restores it. This means the new Untracked memory across all threads field is least accurate on the path it was added for.
    Suggested fix: keep the pre-existing buffered amount visible to the formatter on the throwing path, or pass the current thread's pre-flush contribution into the exception formatting directly instead of recomputing it from the registry after the counter was zeroed.

  • src/Common/MemoryTrackerSwitcher.cpp:24 MemoryTrackerSwitcher still hides this thread's pre-switch buffered bytes from UntrackedMemoryRegistry::sum for the entire switched scope. The constructor saves prev_untracked_memory, publishes 0 in the registered counter, and restores the saved bytes only in the destructor. Any async-metric sample or MEMORY_LIMIT_EXCEEDED message taken inside the switched scope will therefore omit up to one thread buffer per switched thread.
    Suggested fix: keep the saved pre-switch bytes in a registered counter that sum can still see, or flush them before switching parents instead of removing them from the registry for the whole scope.

  • tests/queries/0_stateless/04229_untracked_memory_async_metric.sql:1 The added coverage only proves that UntrackedMemory exists and that its value is finite. It would still pass if UntrackedMemoryRegistry::sum always returned 0, if counters were leaked across thread teardown, or if the MEMORY_LIMIT_EXCEEDED message stopped including the registry value. That is weaker than the PR contract.
    Suggested fix: add a focused unit test for UntrackedMemoryRegistry::sum across multiple counters and destruction, plus a small memory-limit exception test that asserts the new message field is populated with the expected pre-existing buffered bytes.

Final Verdict

⚠️ Changes requested

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 3, 2026

-- Value is an Int64 sum; can be negative if threads have net-freed since last flush,
-- but the metric must exist and be queryable as a number.
SELECT isFinite(value) FROM system.asynchronous_metrics WHERE metric = 'UntrackedMemory';

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 coverage here only proves that UntrackedMemory exists and is finite. It would still pass if UntrackedMemoryRegistry::sum always returned 0, if counters were not removed on thread teardown, or if MEMORY_LIMIT_EXCEEDED exceptions stopped including the registry value. Since the PR contract is specifically that the metric reports the sum of per-thread untracked counters and that this value is surfaced in the exception text, please add focused coverage for those semantics, for example a small unit test that creates multiple UntrackedMemoryCounters with positive and negative deltas and verifies sum before and after one counter is destroyed, plus a minimal memory-limit exception check for the new message field.

@azat azat self-assigned this Jun 3, 2026
Comment thread src/Common/UntrackedMemoryRegistry.cpp Resolved
@mstetsyuk
mstetsyuk requested a review from azat June 4, 2026 19:06
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jun 4, 2026
{
Int64 current_untracked_memory = current_thread->untracked_memory;
current_thread->untracked_memory = 0;
current_thread->untracked_memory.store(0);

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 new exception field can under-report the current thread's saved untracked bytes on the path that needs it most. This stores the registered counter as 0 before memory_tracker->allocImpl formats MEMORY_LIMIT_EXCEEDED; if that call throws, UntrackedMemoryRegistry::sum runs while the current thread's previous buffered value is neither tracked (the allocation was reverted) nor registered, and it is restored only in the catch below. As a result, Untracked memory across all threads can omit this thread's saved buffer.

Please keep the saved current-thread amount visible to the registry during the throwing path, or pass it into the exception formatting, and cover this with a focused MEMORY_LIMIT_EXCEEDED message test.

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.

I don't think this will make any difference in reality, but just to eliminate one source of imprecision, I've fixed this.

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 is still present at src/Common/CurrentMemoryTracker.cpp:82: the counter is stored as 0 before memory_tracker->allocImpl reaches the MEMORY_LIMIT_EXCEEDED formatting path at src/Common/MemoryTracker.cpp:452. If new_untracked_memory == previous + size and the allocation is rejected, the previous buffered bytes are restored only in the catch at src/Common/CurrentMemoryTracker.cpp:90, after the exception text has already called UntrackedMemoryRegistry::sum.

Comment thread src/Common/UDFProcessSubtreeSampler.cpp Resolved
Comment thread src/Server/HTTPHandler.cpp Resolved
Comment thread src/IO/HTTPChunkedReadBuffer.cpp Resolved
Comment thread src/Common/UDFProcessSubtreeSampler.cpp Resolved
Comment thread src/IO/LimitReadBuffer.cpp Resolved
@mstetsyuk mstetsyuk removed the submodule changed At least one submodule changed in this PR. label Jun 5, 2026
prev_memory_tracker_parent = thread_tracker->getParent();

current_thread->untracked_memory = 0;
current_thread->untracked_memory.store(0);

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.

UntrackedMemory can still omit this thread while a MemoryTrackerSwitcher is active. At entry we move the previous counter value into prev_untracked_memory and publish 0 in the registered counter, but those saved bytes are neither flushed to the old parent nor visible to UntrackedMemoryRegistry::sum until the destructor restores them. Any async metric sample or MEMORY_LIMIT_EXCEEDED message inside the switcher scope can under-report by up to this thread's max_untracked_memory, per switched thread.

Please either flush the old counter before switching parents, or keep the saved value in a registered counter that sum includes.

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.

Again, marginal value at best, but we can simplify the code and rule out one source of imprecision, so I've applied a "fix".

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 still holds in current head: src/Common/MemoryTrackerSwitcher.cpp:16 saves the old value and src/Common/MemoryTrackerSwitcher.cpp:20 publishes 0; the saved bytes are not restored until src/Common/MemoryTrackerSwitcher.cpp:36. Any UntrackedMemoryRegistry::sum during the switched scope still omits the pre-switch buffer.

Comment thread src/Common/CurrentMemoryTracker.cpp Resolved
@mstetsyuk

Copy link
Copy Markdown
Member Author

build broken here: #106102

alexey-milovidov pushed a commit to evillique/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to jh0x/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to adityachopra29/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to abonander/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to melvynator/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to Onyx2406/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to Onyx2406/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
Comment thread src/Common/CurrentMemoryTracker.cpp Outdated
std::ignore = memory_tracker->free(-current_untracked_memory, /*_sample_probability=*/ 0.0);
std::ignore = memory_tracker->free(-new_untracked_memory, /*_sample_probability=*/ 0.0);

current_thread->untracked_memory.store(0);

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.

Why you moved it after allocImpl?
This is wrong, we cannot do this because allocImpl may do new allocations, and we will loose them.

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.

allocImpl may do new allocations, and we will loose them

You are right. I've changed store(0) to add(-previous_untracked_memory).

Why you moved it after allocImpl?

If we throw MEMORY_LIMIT_EXCEEDED, we want the message to have a slightly more correct untracked memory amount. So the current thread's untracked memory counter should be previous_untracked_memory (which is the amount of untracked memory before allocation) and not 0.

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.

we want the message to have a slightly more correct untracked memory amount

This is good catch, but it is not correct, look:

untracked_memory=1000 # limit 1001
alloc(32)
untracked_memory=1032 # prev = 1000
untracked_memory=1000
allocImpl() # recursive allocation
    untracked_memory=1000
    alloc(64)
    untracked_memory=1064 # prev = 1000
    untracked_memory=1000
    allocImpl() # no recursive allocation
    untracked_memory-=1000 # 0
untracked_memory-=1000 # -1000

The rule of thumb is that "reset untracked_memory" before calling MemoryTracker

Memory tracker bugs hard to find, so let's just convert one counter to another and leave other parts as-is for now.

Comment on lines -16 to -21
Int64 prev_untracked_memory = 0;
VariableContext prev_untracked_memory_blocker_level = VariableContext::Max;
/// The per-CPU contribution accounts for the same un-flushed bytes as untracked_memory,
/// so it must be saved/restored together with it; otherwise allocations in the switched
/// scope fold into the previous parent's contribution.
PerCPUMemoryThreadState prev_per_cpu;

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.

Can you please explain?

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.

CurrentMemoryTracker::allocImpl computes the per-allocation sampling decision from the cached fields in ThreadStatus via getEffectiveSampleProbability, not by walking the tracker chain every time. MemoryTrackerSwitcher changes the thread tracker's parent, and different parents can resolve to different memory_profiler_* settings, so the switched scope has to re-resolve that cache on entry and restore it on exit or it will keep sampling with the old parent's policy. The removed untracked_memory / per-CPU saved state is a separate change: MemoryTracker::setParent already flushes that state before rewiring the parent, so the switcher no longer needs to preserve it manually.

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.

We flush untracked memory in both ctor (via setParent) and dtor (explicitly) of MemoryTrackerSwitcher.

@azat azat Jul 7, 2026 •

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.

Let's not do this change here and dot it separately, it will change the behavior slightly:

  • it won't restore prev_untracked_memory_blocker_level
  • it will do more flushes, right now we do not flush on first setParent
  • though prev_per_cpu looks redundant here (after removal of "prev_untracked_memory"), but anyway let's do it separately

@mstetsyuk
mstetsyuk requested a review from azat July 6, 2026 09:05
@azat

azat commented Jul 6, 2026

Copy link
Copy Markdown
Member

BTW looks like (at least in CI) we do not have a lot of untracked memory, even w/o #106055, only 99-125MiBs

Details
clickhouse-cloud$  :) SELECT pull_request_number, max(value) FROM merge(default, '^asynchronous_metric_log') WHERE (event_date >= today()-61) AND (pull_request_number in (106386, 104690)) AND metric = 'UntrackedMemory' group by all

SELECT
    pull_request_number,
    max(value)
FROM merge(default, '^asynchronous_metric_log')
WHERE (event_date >= (today() - 61)) AND (pull_request_number IN (106386, 104690)) AND (metric = 'UntrackedMemory')
GROUP BY ALL

Query id: 6070d297-bb9b-471e-ae10-336142374afc

┌─pull_request_number─┬─max(value)─┐
1. │              104690 │   99679558 │ -- 99.68 million
2. │              106386 │  125611079 │ -- 125.61 million
└─────────────────────┴────────────┘

@azat azat 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.

Looks good now, but please take a look at this as well:

  • Changelog entry does not looks user friendly (and even devs friendly)
  • Do we need to join thread pool's in client? Looks like we do not (so as in format, keeper-client, zookeeper-dump-tree, zookeeper-remove-by-list, extract-from-config, keeper-utils) - Ok to do separately (since initially it was done in a separate PR at least)

Comment thread src/Common/UntrackedMemoryRegistry.cpp Resolved
catch (...)
{
current_thread->untracked_memory += previous_untracked_memory;
current_thread->untracked_memory.add(previous_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.

current_thread->untracked_memory is restored here after a failed flush, but the matching untracked_memory_blocker_level is not. That matters because the throwing path in MemoryTracker::allocImpl explicitly expects recursive allocations from Exception::ctor/std::string under MemoryTrackerBlockerInThread(VariableContext::Global) (src/Common/MemoryTracker.cpp:455), and those recursive CurrentMemoryTracker::allocImpl calls update the thread's blocker level to Global.

After this catch rethrows, the restored pre-existing bytes are still buffered, but the next flushUntrackedMemory will use that stale Global blocker and bypass the query/user/process trackers entirely, charging those bytes only to total_memory_tracker. Please restore the saved blocker context together with the saved buffer (keeping the internal exception-construction bytes separate), or flush the internal Global bytes before restoring the caller's state.

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.

Good finding, but not related to this PR, will be fixed separately once this will be merged. Interesting maybe this was the cause of #88483, though AFAIR there were a lot of them, while this situation is rare.

@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 28.60% 85.80% +57.20%
Functions 39.40% 92.70% +53.30%
Branches 20.60% 78.00% +57.40%

Changed lines: Changed C/C++ lines covered: 108/118 (91.53%) · Uncovered code

Full report · Diff report

@azat
azat enabled auto-merge July 17, 2026 11:00
@azat
azat added this pull request to the merge queue Jul 17, 2026
Merged via the queue into master with commit d7ab988 Jul 17, 2026
174 of 176 checks passed
@azat
azat deleted the untracked-memory-race branch July 17, 2026 11:46
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 17, 2026
@Algunenano

Copy link
Copy Markdown
Member

This appears to be broken:

2026-07-21 00:04:30 Code: 241. DB::Exception: Received from localhost:9000. DB::Exception: Received from 127.2:9000. DB::Exception: Query memory limit exceeded: would use 513.05 MiB (attempt to allocate chunk of 512.00 MiB), maximum: 100.00 MiB. Untracked memory across all threads: -1.92 MiB: While executing AggregatingTransform. (MEMORY_LIMIT_EXCEEDED)

It's reporting -1.9MB untracked memory.

@azat

azat commented Jul 28, 2026

Copy link
Copy Markdown
Member

It's reporting -1.9MB untracked memory.

This is OK, it can be from (-threshold, threshold)
Where limit = max(32KiB*threads, 8MiB*cpus)

For 1.9MiB it is ~60 threads that has it's uncommitted freed memory near to the threshold

@azat

azat commented Jul 28, 2026

Copy link
Copy Markdown
Member

@Algunenano

Copy link
Copy Markdown
Member

I didn't realize we could have uncommitted free memory. Makes sense

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-race

Reapply "Add `UntrackedMemory` asynchronous metric"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance 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.

4 participants