Skip to content

Do not correct the global memory tracker below jemalloc stats.allocated - #126176

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-flaky-test-memory-limit
Oct 21, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-flaky-test-memory-limit

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Since #118094 enabled memory_worker_correct_memory_tracker by default, the memory worker resets the global memory tracker to the resident memory on every tick. The resident memory does not include memory that is allocated but not touched yet, so concurrent queries were admitted past max_server_memory_usage. In test_memory_limit/test.py::test_multiple_queries on arm, the container with a 4 GiB limit was killed by the OOM killer, and the server never threw MEMORY_LIMIT_EXCEEDED: three queries reported 1.5 GiB each while the global tracker peaked at 3.6 GiB. 64 KiB pages on the arm runners make the gap larger.

Correcting only upwards would bring back the problem that #118094 fixes: the drift is upwards, and only a downward correction removes it. Instead, the tracker is now corrected to the maximum of the resident memory and jemalloc's stats.allocated. Every allocation of Allocator goes through jemalloc, so stats.allocated includes memory that is allocated but not touched yet. Phantom drift, meaning bytes counted in the tracker with no allocation behind them, is still removed. Sanitizer builds keep using __sanitizer_get_current_allocated_bytes.

The previous commit of this PR disabled the correction in the test. That override is reverted, and the test now passes with the correction enabled: 4 out of 4 local runs on aarch64.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=85c3bde0916bfd25994ff4636169741f354d5fd9&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%203%2F4%29

Related: #118094

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/126176]

Version info

  • Merged into: 26.11.1.382-master (included in 26.11 and later)
  • Backported to: 26.10.7.22, 26.8.41.13

Since `memory_worker_correct_memory_tracker` is enabled by default, the memory
worker resets the global memory tracker to RSS. RSS does not include memory that
is allocated but not yet touched, so the concurrent `GROUP BY` queries of
`test_multiple_queries` can allocate more than `max_server_memory_usage`, and the
container is killed by the OOM killer (seen on arm with 64 KiB pages). The test
keeps checking the behavior with the correction disabled.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=85c3bde0916bfd25994ff4636169741f354d5fd9&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%203%2F4%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac2bb17]

Summary: ✅

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, targeted) SKIPPED CIDB timeout in test selection, rerun to test

AI Review

Summary

The PR corrects the global memory tracker to the maximum of measured resident memory and jemalloc's stats.allocated, preserving accounting for untouched allocations while still removing upward accounting drift. Reviewed head ac2bb17a9740c90e671266b130f26a93806a979d; no actionable correctness or safety findings.

Missing context / blind spots
  • ⚠️ Tests were not run locally. The PR reports four successful aarch64 runs of test_memory_limit/test.py::test_multiple_queries; the current-head integration jobs were still pending at review time, so those results were not independently confirmed.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-testing Test content only (no production code). labels Oct 16, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

This is not good... Maybe we should only ever correct upwards?

…ted`

The memory worker corrected the tracker to the resident memory, which does not include memory that is
allocated but not touched yet. Concurrent queries were admitted past `max_server_memory_usage`, and in
`test_memory_limit/test.py::test_multiple_queries` the container was killed by the OOM killer instead
of the server throwing `MEMORY_LIMIT_EXCEEDED`.

Correct to the maximum of the resident memory and jemalloc `stats.allocated`. All allocations go
through jemalloc, so phantom drift (tracked bytes without an allocation behind them) is still removed,
while allocated-but-untouched memory stays accounted. Revert the override in the test, which now runs
with the correction enabled.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=85c3bde0916bfd25994ff4636169741f354d5fd9&name_0=MasterCI&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%203%2F4%29
Review: #126176 (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov changed the title Disable the memory tracker correction in test_memory_limit Do not correct the global memory tracker below jemalloc stats.allocated Oct 16, 2026
@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... and removed pr-ci comp-testing Test content only (no production code). labels Oct 16, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126176&sha=e583caaad9cdafafcf5566cbadbf13ee1114e580&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 (Logical error: Part all_1_2_1 intersects previous part all_1_1_1 while loading tables in Upgrade check (amd_release)) and provide a fix in a separate PR. If the fix is already in progress, link it here. In the last 30 days it failed this way in about 25 unrelated PRs and in master stress tests, and this PR only changes MemoryWorker. I could not find a tracking issue.

On the review question "maybe we should only ever correct upwards?": correcting only upwards would bring back the drift that #118094 fixes, because that drift is upwards and only a downward correction removes it. So the current commit still corrects downwards, but never below jemalloc stats.allocated. That keeps memory which is allocated but not touched yet accounted. I merged master into the branch to refresh CI.

@groeneai

Copy link
Copy Markdown
Collaborator

Not caused by this PR. The stress phase ran the previous release, v26.9.19.9. There, TRUNCATE TABLE test_alias (03636_storage_alias_access_rights.sh, an Alias table over test_table) hit an injected MEMORY_LIMIT_EXCEEDED in MetadataOperationsHolder::commit, and the rollback left its empty part all_1_1_1 on disk. 0.3 s later a merge of all_1_1_0 and all_2_2_0 produced all_1_2_1, and the upgraded server aborts while loading test_1.test_table.

This is the rolled-back empty part leak that #122375 fixes on master (merged 2026-10-09), so no new PR or tracking issue is needed. The check upgrades from the newest release older than the tested version (ci/tools/get_previous_release_tag.py), so it stays red while that release lacks #122375: neither v26.9.21.9 (released today) nor the 26.9 branch has it. The 26.10 branch does, so the failure stops with the first 26.10 release, or earlier with a 26.9 backport. The master stress test failures with this message all predate #122375 (the last one is from 2026-10-03); the later ones ran on the 26.9 branch.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. That makes sense: the leaked rolled-back empty part comes from the previous release, and #122375 fixes it on master, so this PR does not need to change anything for it. The latest CI run on ac2bb17a is green.

@alexey-milovidov alexey-milovidov left a comment

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.

All good now.

@alexey-milovidov alexey-milovidov self-assigned this Oct 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into master with commit 91e1716 Oct 21, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-flaky-test-memory-limit branch October 21, 2026 04:27
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
Algunenano pushed a commit to Algunenano/ClickHouse that referenced this pull request Oct 22, 2026
…rallel formatting

The setup INSERT writes 2000 one-row CSV objects with PARTITION BY. Until the
sink finishes, every partition keeps a 1 MiB WriteBufferFromS3 buffer and a
ParallelFormattingOutputFormat whose units are resized to 1 MiB, so the query
peaks at 5.90 GiB for 8 KB of data. Since ClickHouse#126176 the global memory tracker is
not corrected below jemalloc's allocated bytes, so on the 12 GiB arm runner it
reaches max_server_memory_usage while RSS is ~1.6 GiB, and the INSERT is
stopped by the OvercommitTracker (Integration tests (arm_binary, 3/4)).

With output_format_parallel_formatting = 0 the INSERT peaks at 1.98 GiB. The
test still writes 2000 objects (two ListObjectsV2 pages) and the SELECT side
is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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 Nov 15, 2026
@fm4v fm4v added v26.8-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels Nov 15, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 15, 2026
robot-clickhouse added a commit that referenced this pull request Nov 18, 2026
Cherry pick #126176 to 26.8: Do not correct the global memory tracker below jemalloc `stats.allocated`
robot-clickhouse added a commit that referenced this pull request Nov 18, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #126176 to 26.10: Do not correct the global memory tracker below jemalloc `stats.allocated`
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #126176 to 26.8: Do not correct the global memory tracker below jemalloc `stats.allocated`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants