Skip to content

Fix SLRU free-space-keeping crash when probationary queue is empty - #104313

Merged
kssenii merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-slru-free-space-keeping-104307
May 8, 2026
Merged

kssenii merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-slru-free-space-keeping-104307

Conversation

@groeneai

@groeneai groeneai commented May 7, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #104307.

Motivation

The background freeSpaceRatioKeepingThreadFunc enforces
keep_free_space_size_ratio / keep_free_space_elements_ratio by
calling SLRUFileCachePriority::collectEvictionInfo with
is_total_space_cleanup=true. The function had a chassert requiring
that we evict at least one byte or element from the probationary queue.

This is wrong: when entries have all been promoted to the protected
queue (probationary empty) and the free-space target asks the cache to
evict everything, the function must still be allowed to evict from the
protected queue. Without the fix the server aborts in debug/sanitizer
builds and throws LOGICAL_ERROR in release builds. The bug is
reachable with the user-facing config in #104307:

<disk1>
  <type>cache</type>
  <disk>disk0</disk>
  <max_size>1Gi</max_size>
  <keep_free_space_elements_ratio>1.0</keep_free_space_elements_ratio>
</disk1>

Approach

Removed the over-zealous assertion in
SLRUFileCachePriority::collectEvictionInfo. The downstream code
already handles the empty-probationary case correctly: it passes zeros
to probationary_queue.collectEvictionInfo (which returns an empty info
immediately) and routes the full requested amount to
protected_queue.collectEvictionInfo.

Added a regression unit test
FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly in
src/Interpreters/tests/gtest_filecache.cpp that:

  1. Creates an SLRU cache with keep_free_space_elements_ratio = 1.0.
  2. Reads a file twice so all entries get promoted to the protected
    queue (probationary stays empty).
  3. Calls freeSpaceRatioKeepingThreadFunc directly.
  4. Without the fix, this aborts with the exact stack trace from Logical error: 'evict_size_from_probationary || evict_elements_from_probationary' #104307
    (SLRUFileCachePriority.cpp:217 -> FileCache.cpp:1446). With the
    fix the call returns cleanly and all 3 protected entries are evicted
    in one batch.

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):

Fix a server abort/LOGICAL_ERROR in the filesystem cache background eviction thread (SLRUFileCachePriority::collectEvictionInfo) that could fire when keep_free_space_size_ratio or keep_free_space_elements_ratio was high enough to trigger eviction while all cache entries had already been promoted to the SLRU protected queue (probationary empty).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Session: cron:clickhouse-ci-task-worker:20260507-164500

Version info

  • Merged into: 26.5.1.453
  • Backported to: 26.4.3.4

`SLRUFileCachePriority::collectEvictionInfo` had a `chassert` requiring
that `evict_size_from_probationary || evict_elements_from_probationary`
in the `is_total_space_cleanup` branch. This is wrong when the
probationary queue is empty but the protected queue still has entries:
the function must still be able to evict from the protected queue.

This is reachable in production via the
`keep_free_space_size_ratio` / `keep_free_space_elements_ratio`
features. The background `freeSpaceRatioKeepingThreadFunc` aborts the
server (debug/sanitizer) or throws `LOGICAL_ERROR` (release) whenever
all entries have been promoted to the protected queue and the free-space
target asks the cache to evict them.

Removed the assertion. The downstream code already handles the case
correctly: it passes zero size/elements to `probationary_queue.collectEvictionInfo`
(which returns an empty info immediately) and routes the full requested
amount to the protected queue.

Added a regression unit test `FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly`
that reproduces the exact stack trace from the issue.

Closes ClickHouse#104307
@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

a) Deterministic repro? Yes — the new unit test FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly triggers the abort 100% of the time without the fix.

b) Root cause explained? Yes. With keep_free_space_elements_ratio = 1.0 the background thread targets desired_elements_num = 0. After all cache entries are promoted to the SLRU protected queue (probationary empty), the thread calls collectEvictionInfo(0, N>0, ..., is_total_space_cleanup=true). Inside the function evict_*_from_probationary is min(., 0) = 0, and the chassert at SLRUFileCachePriority.cpp:217 fires.

c) Fix matches root cause? Yes — removed the wrong assertion. The downstream code already handles (0, 0) correctly: it passes zeros to probationary_queue.collectEvictionInfo (empty info returned immediately) and routes the full requested amount to the protected queue.

d) Test intent preserved? / New tests added? Existing tests are unchanged. Added FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly which is a focused regression test for #104307.

e) Demonstrated in both directions? Yes:

f) Fix is general, not a narrow patch? Yes — same bug applies for keep_free_space_size_ratio (size variant) and any state where probationary is empty during a is_total_space_cleanup call. Removing the assertion covers all of them. No symmetric LRUFileCachePriority::collectEvictionInfo bug exists (it has no such assertion). The fix addresses the root cause (the assertion was simply wrong about the invariant); it does not just guard a symptom.

Session: cron:clickhouse-ci-task-worker:20260507-164500

@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @kssenii @alesapin — could you review this? It removes a wrong chassert in SLRUFileCachePriority::collectEvictionInfo that aborts the server (debug/sanitizer) or throws LOGICAL_ERROR (release) whenever the SLRU probationary queue is empty while the protected queue still has entries — reproducible via #104307 with keep_free_space_elements_ratio = 1.0. The downstream code already handles the empty-probationary case correctly. Includes a focused unit test that reproduces the exact stack trace.

@kssenii kssenii self-assigned this May 7, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6774371]

Summary: ✅


AI Review

Summary

This PR removes an invalid assertion in SLRUFileCachePriority::collectEvictionInfo for total-space cleanup when the probationary queue is empty, and adds a focused regression test covering the protected-only eviction path. I did not find correctness, safety, or compatibility issues in the patch.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.70% 76.60% -0.10%

Changed lines: 100.00% (50/50) · Uncovered code

Full report · Diff report

@kssenii
kssenii added this pull request to the merge queue May 8, 2026
Merged via the queue into ClickHouse:master with commit 74e1c5e May 8, 2026
165 checks passed
robot-clickhouse added a commit that referenced this pull request May 8, 2026
Cherry pick #104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
robot-clickhouse added a commit that referenced this pull request May 8, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-synced-to-cloud The PR is synced to the cloud repo pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels May 8, 2026
kssenii added a commit that referenced this pull request May 9, 2026
Backport #104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 9, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 11, 2026
The original test (added in ClickHouse#104313 to cover ClickHouse#104307) drove the bug condition through
`FileCache::freeSpaceRatioKeepingThreadFunc` with `keep_free_space_elements_ratio = 1.0`.
That setting causes `cache->initialize()` to schedule the asynchronous background
eviction task at `FileCache.cpp:444`, which then races with the test's `read_and_check`
and intermediate `assertProbationary` / `assertProtected` calls. On slow builds (notably
`Unit tests (amd_llvm_coverage)`) the background thread evicts entries between the
populate and assert steps, causing the test to fail spuriously even though the underlying
fix is correct.

Confirmed master-branch failures on commits `74e1c5ed` (16:03:32 UTC, line 1626 mismatch)
and `407d6cca` (16:24:46 UTC, `assertProtected` mismatch at line 214 from line 1616).
The same failure pattern reproduces in PR run for ClickHouse#89334 (b5c32ee, line 214 mismatches).
All failures are exclusive to the coverage shard; tsan / asan_ubsan / msan all pass.

The replacement test exercises `SLRUFileCachePriority::collectEvictionInfo` directly,
constructing the priority queue and adding three protected-only entries via
`addForRestore(... QueueEntryType::SLRU_Protected ...)`. This reproduces the exact bug
condition (probationary empty, protected populated, `is_total_space_cleanup=true`) without
involving the file cache's asynchronous machinery, making it deterministic across all
build flavors.

Verified locally:
- With the fix in `SLRUFileCachePriority::collectEvictionInfo`, the test passes 10/10
  in 0 ms.
- Reintroducing the original `chassert` aborts the unit test process with `Logical
  error: 'evict_size_from_probationary || evict_elements_from_probationary'` and exit
  code 134 (SIGABRT), confirming the test still catches the regression.
- All 11 `FileCacheTest.*` tests still pass.

CI failures observed in
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=74e1c5ed2c4437352b486d59b16ee8bd98a8b688&name_0=MasterCI&name_1=Unit%20tests%20%28amd_llvm_coverage%29
and
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=407d6ccaeb432fbd86b15dc6d341651a64f447dc&name_0=MasterCI&name_1=Unit%20tests%20%28amd_llvm_coverage%29
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…4313

Backport ClickHouse#104313 to 26.4: Fix SLRU free-space-keeping crash when probationary queue is empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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 v26.2-must-backport v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: 'evict_size_from_probationary || evict_elements_from_probationary'

5 participants