Skip to content

Fix incorrect memory around max_untracked_memory - #83607

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:memory-untracked_memory-fix
Jul 13, 2025
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:memory-untracked_memory-fix

Conversation

@azat

@azat azat commented Jul 10, 2025 •

Copy link
Copy Markdown
Member

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

Fix incorrect memory around max_untracked_memory

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Due to one tiny allocation inside MemoryTracker for logs, recursive allocation from CurrentMemoryTracker -> MemoryTracker -> CurrentMemoryTracker is possible, which will lead to double accounting of untracked_memory (this is happens after #81694, before the problem was also there, but it just ignores the inner allocation).

Thanks to @filimonov for the provided test - horizontal merges for wide tables with wide parts (system.metric_log) for a few minute (3-5) will lead to ~3GiB of memory drift.

And the rule of thumb is - any accounting of per-thread untracked_memory should be done before going into MemoryTracker.

Note, that this is the simplest possible fix for now (that will be easy to backport), I will do some refactoring and cleanup later.

Fixes: #82036
Fixes: #83352
Fixes: #82627

Refs:

  • Possible stacktrace (obtained with extra patch that adds some sanity checks that leads to endless recursion)

@azat
azat requested review from al13n321 and antonio2368 July 10, 2025 21:05
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bc66425]

Summary: ❌

job_name test_name status info comment
Build (fuzzers) error
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, 2/2) failure
Logical error thrown (see clickhouse-server.log or logical_errors.txt) FAIL
Finish Workflow failure
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py failure

Comment thread src/Common/CurrentMemoryTracker.cpp Resolved
@al13n321 al13n321 self-assigned this Jul 10, 2025
@azat

azat commented Jul 11, 2025 •

Copy link
Copy Markdown
Member Author

@azat
azat force-pushed the memory-untracked_memory-fix branch from 36f7747 to 8c07726 Compare July 11, 2025 06:39
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 11, 2025
@azat
azat force-pushed the memory-untracked_memory-fix branch from 8c07726 to 6d2c6e6 Compare July 11, 2025 08:28
@azat
azat force-pushed the memory-untracked_memory-fix branch from 6d2c6e6 to 5a5aad7 Compare July 11, 2025 16:38
The problem is one tiny allocation inside MemoryTracker, due to which we
can call CurrentMemoryTracker recursively, which will lead to double
accounting of `untracked_memory`, after ClickHouse#81694.

But incorrect memory tracking was there even w/o this PR, since w/o it
will ignore inner allocations in `untracked_memory`.

Note, that this is the simplest possible fix for now (that will be easy
to backport), I will do some refactoring and cleanup later.

Basically any accounting of per-thread `untracked_memory` should be done
**before** going into `MemoryTracker`.

Thanks to @filimonov for the provided test - horizontal merges for wide
tables with wide parts (`system.metric_log`) for copule of minute (3-5)
will lead to ~3GiB of memory drift.

v2: proper accounting in case of exception

Fixes: ClickHouse#82036
Fixes: ClickHouse#83352
Fixes: ClickHouse#82627
@azat
azat force-pushed the memory-untracked_memory-fix branch from 5a5aad7 to bc66425 Compare July 11, 2025 17:29
@azat azat changed the title Fix incorrect memory tracking Fix incorrect memory tracking due to max_untracked_memory and recursive allocations Jul 12, 2025
@azat

azat commented Jul 12, 2025

Copy link
Copy Markdown
Member Author

Stateless tests (amd_asan, distributed plan, 1/2) — Failed: 1, Passed: 4026, Skipped: 77

  • 01576_alter_low_cardinality_and_select

@azat
azat enabled auto-merge July 12, 2025 22:26
@azat
azat added this pull request to the merge queue Jul 12, 2025
@azat
azat removed this pull request from the merge queue due to a manual request Jul 12, 2025
@azat azat changed the title Fix incorrect memory tracking due to max_untracked_memory and recursive allocations Fix incorrect memory around max_untracked_memory Jul 12, 2025
@azat

azat commented Jul 12, 2025 •

Copy link
Copy Markdown
Member Author

I finally found another issue that I have observed from time to time, this is not that general though, when the user set max_untracked_memory=0 all previous allocations (up to 4MB) may be lost, since we did not flush it properly, fix - 3fd3c13

TL;DR; I think now, the total memory tracker should be correct.

@azat
azat force-pushed the memory-untracked_memory-fix branch from 3fd3c13 to bc66425 Compare July 12, 2025 22:56
@azat
azat enabled auto-merge July 13, 2025 17:20
clickhouse-gh Bot added a commit that referenced this pull request Jul 13, 2025
Backport #83607 to 25.6: Fix incorrect memory around max_untracked_memory
azat added a commit that referenced this pull request Jul 14, 2025
Backport #83607 to 25.3: Fix incorrect memory around max_untracked_memory
azat added a commit that referenced this pull request Jul 14, 2025
Backport #83607 to 25.4: Fix incorrect memory around max_untracked_memory
azat added a commit that referenced this pull request Jul 14, 2025
Backport #83607 to 25.5: Fix incorrect memory around max_untracked_memory
zvonand pushed a commit to Altinity/ClickHouse that referenced this pull request Jul 14, 2025
…-fix

Fix incorrect memory around max_untracked_memory
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 14, 2025
azat added a commit that referenced this pull request Jul 15, 2025
Backport #83607 to 24.8: Fix incorrect memory around max_untracked_memory
@filimonov

Copy link
Copy Markdown
Contributor

After the change :|

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-backports-created-cloud deprecated label, NOOP pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

7 participants