Skip to content

fix(commons): clear LRUCache pointers on eviction of last item - #5295

Merged
svozza merged 1 commit into
aws-powertools:mainfrom
vishwakt:fix/lru-cache-max-size-zero
May 26, 2026
Merged

svozza merged 1 commit into
aws-powertools:mainfrom
vishwakt:fix/lru-cache-max-size-zero

Conversation

@vishwakt

@vishwakt vishwakt commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

When LRUCache is configured with maxSize 0, adding any item triggers immediate eviction. When the evicted item has no newer neighbor, shift() was not clearing the leastRecentlyUsed and mostRecentlyUsed pointers. Subsequent additions would incorrectly link onto these stale pointers, causing the cache to permanently retain one stale entry.

This change sets both pointers to undefined in shift() when the evicted item is the only one in the cache.

Issue number: closes #5290


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When LRUCache is configured with maxSize 0, adding an item triggers immediate eviction. If the evicted item has no newer neighbor, shift() was not clearing the leastRecentlyUsed and mostRecentlyUsed pointers. Subsequent additions would incorrectly link onto these stale pointers, causing the cache to permanently retain one stale entry.

Clear both pointers in shift() when the evicted item is the only one in the cache. Also add test coverage for multiple additions with maxSize 0.

Closes aws-powertools#5290
@svozza
svozza merged commit acf1ba1 into aws-powertools:main May 26, 2026
36 checks passed
@svozza

svozza commented May 26, 2026

Copy link
Copy Markdown
Contributor

Merged!

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

Labels

size/XS PR between 0-9 LOC

2 participants