Skip to content

fix(unsync): fix total weight tracking leak upon entry expiration in unsync Cache#55

Open
Jay-1409 wants to merge 1 commit into
moka-rs:v0.11.xfrom
Jay-1409:v0.11.x
Open

fix(unsync): fix total weight tracking leak upon entry expiration in unsync Cache#55
Jay-1409 wants to merge 1 commit into
moka-rs:v0.11.xfrom
Jay-1409:v0.11.x

Conversation

@Jay-1409

@Jay-1409 Jay-1409 commented Jun 11, 2026

Copy link
Copy Markdown
  • remove_expired_wo was most probably incorrectly using saturating_sub
  • I have also added a test to check for the same.

Summary by CodeRabbit

  • Bug Fixes

    • Corrected cache weight accounting during TTL-based entry cleanup to prevent bookkeeping leaks and calculation errors when entries expire.
  • Tests

    • Added unit test verifying that cache weight properly resets after TTL-expired entries are removed, ensuring accurate weight tracking across eviction cycles.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eb653564-5044-485e-96a9-e527a8907e44

📥 Commits

Reviewing files that changed from the base of the PR and between a4305fa and 9cbe7c6.

📒 Files selected for processing (1)
  • src/unsync/cache.rs

📝 Walkthrough

Walkthrough

This PR fixes a weight bookkeeping bug in the TTL eviction path of the unsynchronized cache. The remove_expired_wo function incorrectly subtracted from eviction weight tracking instead of adding to it, causing weight leaks. The fix is paired with a unit test that validates correct behavior.

Changes

TTL Weight Tracking Fix

Layer / File(s) Summary
TTL eviction weight accounting
src/unsync/cache.rs
The accumulated evicted_policy_weight in remove_expired_wo now correctly uses saturating_add instead of saturating_sub to track entries evicted during TTL cleanup. The new test_ttl_weight_leak unit test verifies that weighted_size() drops to zero after a TTL-expired entry is removed, ensuring weight bookkeeping does not leak.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A weight that's evicted must add to the tally,
Not subtract with a subtract—what folly, what folly!
With saturating_add, the ledger stays true,
And test_ttl_weight_leak proves zero shines through! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a weight tracking leak in the unsync Cache when entries expire.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@Jay-1409

Copy link
Copy Markdown
Author

Hi @tatsuya6502

Could you please have a look at this. It would help me proceed in my project. (if it's not a relevant bug)

Thank you so much!!
Jay

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant