Skip to content

fix: don't cache items larger than maxSize when maxEntrySize is larger - #435

Open
BlueX888 wants to merge 3 commits into
isaacs:mainfrom
BlueX888:contrib/maxsize-overflow-accounting
Open

BlueX888 wants to merge 3 commits into
isaacs:mainfrom
BlueX888:contrib/maxsize-overflow-accounting

Conversation

@BlueX888

Copy link
Copy Markdown
Contributor

maxSize documents that an item larger than the limit is not cached and does not evict other items (src/index.ts:915-917). When maxEntrySize is explicitly larger than maxSize, a set() with maxSize < size <= maxEntrySize slips past the maxEntrySize guard into #addItemSize; the eviction loop then runs with a negative budget, evicts the item being measured and other items, and leaves calculatedSize negative. Later entries start from that negative budget, so real bytes can exceed maxSize.

This adds a guard in #set after the existing maxEntrySize check: when maxSize is set and the entry size exceeds it, the write is a no-op — the previous value and accounting are retained, and a background-fetch placeholder is removed. Explicit maxEntrySize rejections, maxSize: 0, size === maxSize, and the update path are unchanged.

Regression coverage in test/size-calculation.test.ts covers a new oversized entry and an oversized update; both fail on 2056b03 with calculatedSize does not equal sum of sizes. npx ubq passes 487 tests; oxlint and prettier are clean.

maxEntrySize defaults to maxSize, but may be configured higher. In that
case an item whose size exceeds maxSize passed the maxEntrySize guard,
and #addItemSize tried to evict down to a negative budget. That evicted
the item being added along with other entries, leaving calculatedSize
negative so that later items could push the real total over maxSize.

Treat size > maxSize as a no-op in #set, preserving any existing value
and its size accounting, as documented for maxSize. Values larger than
maxEntrySize keep the existing behavior. A background fetch placeholder
for the key is dropped, since it is not a cached value.
@BlueX888

BlueX888 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Pushed empty commits (9083d00, 751539d; same tree as bae1f02) to retrigger CI.

The Windows matrix failures are unrelated to this change: they are the time-based assertion in test/onInsert.test.ts ("no onInsert for background fetch promises"), where a cache with ttl: 10 ms must still return the cached 'Y' for fetch('y'); when the runner takes longer than 10 ms between the set and the fetch, the background fetch returns 'y' and the assertion fails. The same test failed on 22.x windows powershell in the first run and on 24.x windows bash in the retrigger, while the new maxSize branch is not executed by that test (the cache uses max only). The empty commits keep the tree identical, so the prior review still applies.

@isaacs

isaacs commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Ok, but why do you have a maxSize that is smaller than maxEntrySize? That feels like it should just throw an error at creation time, because it's obviously a mistake.

@BlueX888

BlueX888 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Good question. The constructor validates each limit independently (positive integer) but never compares them. maxEntrySize defaults to maxSize, so an explicitly larger value only makes the per-entry guard looser than the total; it can never let an item larger than maxSize stay cached. This path came up in a boundary probe of the documented contract rather than from a user report.

When the configuration is set that way, the maxSize docs promise a no-op, but set() currently evicts the entry being measured plus peers and can drive calculatedSize negative, after which maxSize is no longer enforced.

I kept the runtime guard rather than only rejecting the configuration because maxEntrySize is a public, mutable property: a caller can raise it after construction (cache.maxEntrySize = ...), so the no-op behavior still needs to hold at set() time. I am happy to add the constructor check you suggested as well (throw when maxSize and an explicit maxEntrySize are both set and maxEntrySize > maxSize), either in this PR or as a separate change with its own test; say which you prefer.

One behavior detail the guard preserves: the maxEntrySize path deletes an existing value, while maxSize is documented as a no-op, so the two paths stay distinct.

@isaacs

isaacs commented Dec 1, 2026

Copy link
Copy Markdown
Owner

I'm still struggling to understand how this would ever happen in an actual program. Yes, technically these fields are mutable, but does anyone ever change them after being set at construction? Feels like a stretch.

Have you observed it? Or is it strictly coming from analysis of the code?

I'd prefer not to add runtime checks to this module for things that don't happen in reality.

@BlueX888

BlueX888 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Strictly from analysis: I have not observed this in a real program, and there is no user report. I should not have leaned on the mutable-field argument; there is no observed code that changes these after construction. The only way in is an explicit configuration with maxEntrySize > maxSize at construction.

That points at the constructor check you suggested, and I agree it is the better shape: catch the contradiction once instead of carrying a runtime branch for it. I can replace this PR with a narrow constructor validation (throw when an explicit maxEntrySize is larger than maxSize, plus a test and a changeset note) and drop the set() guard. If you would rather not enforce the combination at all, that is fine too; tell me which you prefer and I will follow it.

@isaacs

isaacs commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Yeah, ok, then I think a guard in the constructor is best. Cheap and simple, and avoids the most likely problem. If someone explicitly sets it incorrectly later, they're doing a weird thing and can expect weird results 👍 Thanks for taking this on.

This branch has not been deployed

No deployments
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