Skip to content

Fix filesystem cache race in incrementSize after eviction - #99662

Closed
Algunenano wants to merge 2 commits into
ClickHouse:masterfrom
Algunenano:fix/cache-increment-size-race
Closed

Algunenano wants to merge 2 commits into
ClickHouse:masterfrom
Algunenano:fix/cache-increment-size-race

Conversation

@Algunenano

Copy link
Copy Markdown
Member

Between collectEvictionInfo and incrementSize, the state lock is dropped for eviction I/O. During that gap other threads may complete reserve cycles that change the sub-queue state, so incrementSize can legitimately find that a sub-queue no longer has room. This manifests as a LOGICAL_ERROR exception "Cannot increment size".

Example failure: https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/json.html?PR=52290&sha=c7313ec1fda3fdda8147d312f770824667ffe879&name_0=PR&name_1=Stateless%20tests%20%28amd_release%2C%20meta%20storage%20in%20keeper%2C%20s3%20storage%2C%20sequential%29

Add tryIncrementSize (returns false instead of throwing) to the Iterator interface and use it in doTryReserve to treat the condition as a transient reservation failure. Also fix argument order in LRUFileCachePriority::getStateInfoForLog which swapped max_elements with hold size / hold elements.

cc @kssenii

Changelog category (leave one):

  • CI Fix or Improvement

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

Fix a race condition in the filesystem cache that could cause a LOGICAL_ERROR "Cannot increment size" when multiple threads compete for cache space.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Between `collectEvictionInfo` and `incrementSize`, the state lock is
dropped for eviction I/O. During that gap other threads may complete
reserve cycles that change the sub-queue state, so `incrementSize` can
legitimately find that a sub-queue no longer has room. This manifests
as a LOGICAL_ERROR exception "Cannot increment size".

Add `tryIncrementSize` (returns false instead of throwing) to the
`Iterator` interface and use it in `doTryReserve` to treat the
condition as a transient reservation failure.

Also fix argument order in `LRUFileCachePriority::getStateInfoForLog`
which swapped `max_elements` with `hold size` / `hold elements` in
the diagnostic message.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [13ad746]

Summary: ✅


AI Review

Summary

This PR addresses a real race in file cache reservation accounting: when the state lock is released for eviction I/O, concurrent reservations can consume freed space before incrementSize runs. The change introduces tryIncrementSize to treat this as a transient reservation failure instead of raising a LOGICAL_ERROR, and wires it through LRU/SLRU/Split iterators. I did not find any additional high-confidence correctness, safety, or performance issues in the patch.

Missing context

  • ⚠️ CI is still running (IN_PROGRESS/PENDING checks), so final test signal is incomplete.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 16, 2026
Comment thread src/Interpreters/Cache/FileCache.cpp Resolved
The same post-eviction race window affects the query priority path.
Roll back the main iterator increment on failure.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@kssenii kssenii self-assigned this Mar 16, 2026
/// Between `collectEvictionInfo` (which computed how much to evict under the
/// state lock) and here, the state lock was dropped for eviction I/O. During
/// that gap other threads may have acquired hold spaces or completed reserve
/// cycles that consumed the freed space in a sub-queue, so `incrementSize`

@kssenii kssenii Mar 16, 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.

the state lock was dropped for eviction I/O. During
/// that gap other threads may have acquired hold spaces or completed reserve
/// cycles that consumed the freed space in a sub-queue,

This is not true, see IFileCachePriority::HoldSpace (and how it is used) which is created for this purpose to avoid the described issue. Likely the failure itself is related to private-only feature (a different cache policy which is enabled in private by default). Let me investigate myself, I haven't seen this failure.

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.

Yes, the issue affects only the private feature, but I needed to change the API here

@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.70% -0.10%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 55.38% (36/65, 1 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov

Copy link
Copy Markdown
Member

This fix is much better than the one I merged a minute ago... let's un-draft?

@alexey-milovidov

Copy link
Copy Markdown
Member

@kssenii, what to do with this change?

@Algunenano Algunenano closed this Apr 7, 2026
@kssenii

kssenii commented Apr 8, 2026

Copy link
Copy Markdown
Member

The root cause should be fixed by #101991

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants