Skip to content

fix(cache): evict entries when a key holds a single entry - #5737

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix/memory-cache-store-eviction
Open

fix(cache): evict entries when a key holds a single entry#5737
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix/memory-cache-store-eviction

Conversation

@mcollina

Copy link
Copy Markdown
Member

MemoryCacheStore's eviction used entries.splice(0, entries.length / 2). splice truncates a non-integer deleteCount toward zero, so in the common case of a single cached response per distinct key (entries.length === 1 -> 0.5) nothing was evicted. As a result maxSize and maxCount were never enforced and memory grew unboundedly as distinct URLs were cached.

This changes the eviction to use Math.ceil(entries.length / 2) so a single-entry key always evicts its entry, adds a regression test covering the one-entry-per-key case, and updates the existing limit tests that had encoded the buggy behavior of leaving the store full after overflow.

Reported in GHSA-37v3-qqr5-gmgc.

MemoryCacheStore's eviction used entries.splice(0, entries.length / 2).
splice truncates a non-integer deleteCount toward zero, so for the common
case of a single cached response per distinct key (entries.length === 1 ->
0.5) nothing was evicted. As a result maxSize and maxCount were never
enforced and memory grew unboundedly as distinct URLs were cached.

Use Math.ceil(entries.length / 2) so a single-entry key always evicts its
entry, and add a regression test covering the one-entry-per-key case. Update
the existing limit tests, which had encoded the buggy behavior of leaving the
store full after overflow.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.48%. Comparing base (fc3450d) to head (be12d00).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5737      +/-   ##
==========================================
+ Coverage   93.47%   93.48%   +0.01%     
==========================================
  Files         110      110              
  Lines       38908    38908              
==========================================
+ Hits        36368    36374       +6     
+ Misses       2540     2534       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants