Skip to content

Fix local_cache::free_space skipping the next entry to evict - #7824

Merged
hkaiser merged 1 commit into
TheHPXProject:masterfrom
Vaatsal-source:fix/local-cache-free-space
Oct 22, 2026
Merged

hkaiser merged 1 commit into
TheHPXProject:masterfrom
Vaatsal-source:fix/local-cache-free-space

Conversation

@Vaatsal-source

Copy link
Copy Markdown
Contributor

Fixes #7823

Proposed Changes

  • In local_cache::free_space, point the iterator to the new front of the
    heap after std::pop_heap/pop_back instead of advancing it before the
    pop. Previously the entry that pop_heap moved to the front was skipped,
    the following evictions followed heap array order instead of policy order,
    and an iterator past the end of an emptied heap could be dereferenced.
  • Add regression tests to local_lru_cache.cpp: after shrinking a full LRU
    cache the least recently used entries are the ones evicted (also after
    touching an entry), and inserting an entry larger than the whole cache
    fails without crashing.

Any background context you want to provide?

Without this change, shrinking an LRU cache holding keys 0 to 4 (0 oldest) to
two entries keeps keys 1 and 4 instead of 3 and 4, and inserting an entry of
size 5 into a cache of capacity 3 holding one entry crashes (invalid iterator).

Inserting an entry that is larger than the whole cache still evicts all
existing entries before it fails. It might be preferable to reject such an
entry up front; I left that out of this change.

Checklist

Not all points below apply to all pull requests.

  • I have added a new feature and have added tests to go along with it.
  • I have fixed a bug and have added a regression test.
  • I have added a test using random numbers; I have made sure it uses a seed, and that random numbers generated are valid inputs for the tests.

When the entry at the top of the heap was evicted, free_space() advanced
its iterator before std::pop_heap and pop_back. The entry that pop_heap
moved to the front was therefore skipped, and the following entries were
evicted in heap array order instead of policy order. If the top entry
was the only one left, the advanced iterator became invalid and was
dereferenced while more space was still needed (undefined behavior, for
example when inserting an entry larger than the whole cache).

Point the iterator to the new front of the heap after the pop instead.

Add regression tests.

Fixes TheHPXProject#7823

Signed-off-by: Vaatsalya Srivastava <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2923313a-ceb1-43a1-89e7-92c8f3de1832
📥 Commits

Reviewing files that changed from the base of the PR and between 6456abf and 12ac772.

📒 Files selected for processing: 2
  • libs/core/cache/include/hpx/cache/local_cache.hpp
  • libs/core/cache/tests/unit/local_lru_cache.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Corrected cache space reclamation so it continues with the next eviction candidate instead of potentially skipping it. When the cache shrinks, it now retains the most recently used entries as expected.
    • Confirmed that inserting an entry larger than the cache capacity fails, including when the cache already contains a smaller entry.

local_cache::free_space now resets its candidate iterator to the heap’s new front after removing the current front. New tests check LRU eviction order after shrinking capacity and rejection of an entry larger than the cache capacity.

Changes: Local cache eviction

Layer / File(s) Summary
Heap-front eviction and regression tests
libs/core/cache/include/hpx/cache/local_cache.hpp, libs/core/cache/tests/unit/local_lru_cache.cpp
free_space now selects the new heap front after popping the current candidate. Tests check which LRU entries remain after shrinking the cache and verify that an oversized entry is rejected.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 12ac7

No identified issue blocks merging after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main bug fix in local_cache::free_space.
Description check Passed The description explains the iterator bug, the eviction behavior, the regression tests, and the issue scope. It is directly related to the changeset.
Linked Issues check Passed Issue #7823 requires local_cache::free_space() to evict entries in update-policy order and avoid invalid iterators. The supplied PR evidence states that free_space calls std::pop_heap and `pop_b…
Out of Scope Changes check Passed The reported changes are limited to the local_cache::free_space iterator fix and regression tests in local_lru_cache.cpp. These changes directly support issue #7823. No unrelated production behavi…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Full details: Linked Issues check

Explanation

Issue #7823 requires local_cache::free_space() to evict entries in update-policy order and avoid invalid iterators. The supplied PR evidence states that free_space calls std::pop_heap and pop_back, then resets the iterator to the new heap front. The added tests verify LRU eviction order, including after a touch, and oversized insertion failure without a crash. The PR also preserves the issue's out-of-scope behavior for oversized entries.

Full details: Out of Scope Changes check

Explanation

The reported changes are limited to the local_cache::free_space iterator fix and regression tests in local_lru_cache.cpp. These changes directly support issue #7823. No unrelated production behavior or unrelated tests are identified.

✨ Finishing Touches: 💡 1
🧪 Generate unit tests (beta)
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@StellarBot

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@hkaiser hkaiser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! LGTM, thanks!

@hkaiser hkaiser added this to the 2.0.0 milestone Oct 22, 2026
@hkaiser
hkaiser merged commit 8b614d9 into TheHPXProject:master Oct 22, 2026
80 of 84 checks passed
@hkaiser hkaiser mentioned this pull request Oct 22, 2026
1 of 2 tasks
hkaiser added a commit that referenced this pull request Oct 22, 2026
Restore the release notes update that was lost when release-2.0.X was rebuilt on master eaff5b9, and describe the PRs merged since the previous rebuild (#6514, #7767, #7816, #7817, #7819, #7821, #7824, #7831). Regenerate the closed issue and pull request lists for release-2.0.X at 4cb902e against the v1.11.0 tag (775 PRs, 167 issues).

Signed-off-by: Hartmut Kaiser <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

local_cache::free_space skips the next entry to evict and can dereference past the end

3 participants