Skip to content

workload: fix MemoryReservation hang when increase races with in-flight decrease - #116085

Merged
azat merged 4 commits into
ClickHouse:masterfrom
azat:workload/fix-memory-reservation-increase-decrease-race
Sep 4, 2026
Merged

azat merged 4 commits into
ClickHouse:masterfrom
azat:workload/fix-memory-reservation-increase-decrease-race

Conversation

@azat

@azat azat commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Decreases are approved asynchronously, so an increase computed while one is in flight used a stale allocated value: drop A->B (decrease A-B enqueued), rise to C before approval enqueues C-A; after both approvals allocated = C - (A-B), and the thread waits on actual <= allocated forever (the unchanged-tracker early return prevented re-issuing the shortfall). Fix: track the in-flight decrease size and size requests against the post-decrease allocated value.

Changelog category (leave one):

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

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

workload: fix MemoryReservation hang when increase races with in-flight decrease


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

Version info

  • Merged into: 26.9.1.660 (included in 26.9 and later)

@azat
azat requested a review from serxa August 24, 2026 11:15
@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [178767e]

Summary: ❌

job_name test_name status info comment
Bugfix validation (unit tests) ERROR

AI Review

Summary

This PR fixes a real race in MemoryReservation::syncWithMemoryTracker: when usage regrows while a decrease is still in flight, the compensating increase must be sized against the post-decrease allocation and the sync must wait for that post-decrease coverage before returning. The current code and the added regression tests cover the previously reported hang/under-reservation cases, and I do not see a new correctness or concurrency blocker in the diff.

PR Metadata

Changelog category: correct. This is a Bug Fix.

Changelog entry: required, but the current text is too implementation-centric and not user-readable enough for the changelog. It should describe the user-visible effect rather than internal scheduler fields.

Exact replacement:
Fixes query hangs with workload memory scheduling when reserve_memory is used and memory usage decreases and then grows again before the scheduler applies the pending decrease.

Final Verdict

✅ Code looks correct. No new inline findings to post on the current diff; only the changelog entry should be rewritten into user-facing language.

LLVM Coverage Report

⚠️ No coverage measurement for commit 178767e: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_7.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 178767ee9 with master 41b3bd68a (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of fb0b23675; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

10 object files changed (+56.96 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/StorageMerge.cpp.o 1.18 MiB 1.21 MiB +28.54 KiB (+2.36%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/applyParallelReplicas.cpp.o 185.85 KiB 207.46 KiB +21.62 KiB (+11.63%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

52 translation units recompiled, 440 s compile time in total, 52 of them have a recent master baseline.

Job report

@serxa serxa self-assigned this Aug 31, 2026

@serxa serxa 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, but let's do some small refactoring to make things clear

@azat

azat commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

azat and others added 4 commits September 3, 2026 16:08
…ht decrease

Decreases are approved asynchronously, so an increase computed while one
is in flight used a stale allocated value: drop A->B (decrease A-B
enqueued), rise to C before approval enqueues C-A; after both approvals
allocated = C - (A-B), and the thread waits on actual <= allocated
forever (the unchanged-tracker early return prevented re-issuing the
shortfall). Fix: track the in-flight decrease size and size requests
against the post-decrease allocated value; take the early-return only
when fully in sync, so residual shortfall/excess self-heals.

Co-Authored-By: Claude Fable 5 <[email protected]>
A blocking scheduler event keeps the decrease in flight while the
increase is enqueued, making the crossing deterministic.

Co-Authored-By: Claude Fable 5 <[email protected]>
@azat
azat force-pushed the workload/fix-memory-reservation-increase-decrease-race branch from a921eab to 178767e Compare September 3, 2026 14:08
@azat
azat enabled auto-merge September 3, 2026 14:18
@azat
azat added this pull request to the merge queue Sep 4, 2026
Merged via the queue into ClickHouse:master with commit b0bbe7a Sep 4, 2026
172 of 174 checks passed
@azat
azat deleted the workload/fix-memory-reservation-increase-decrease-race branch September 4, 2026 02:36
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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