fix(image): skip 0-byte entries when initializing disk LRU cache#94068
Draft
hammadxcm wants to merge 1 commit into
Draft
fix(image): skip 0-byte entries when initializing disk LRU cache#94068hammadxcm wants to merge 1 commit into
hammadxcm wants to merge 1 commit into
Conversation
A single 0-byte or corrupt file in .next/cache/images/ produced a size-0 entry. LRUCache.set() rejects sizes <= 0, which threw during disk-LRU initialization and permanently rejected the cached singleton promise, breaking image optimization for the rest of the process. Skip non-positive size entries so one bad file can no longer poison the cache.
4c32eb1 to
640a483
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
A single 0-byte (empty/corrupt) file in
.next/cache/images/permanently breaks image optimization for the rest of the process. This skips entries with a non-positive size when initializing the disk-LRU singleton.Why?
initCacheEntries()reads such a file as asize: 0entry. During init,getOrInitDiskLRU()replays entries vialru.set(entry.key, entry.size), butLRUCache.set()intentionally throws whensize <= 0(guard against unbounded growth). That throw rejects the singleton IIFE, and because_diskLRUPromiseis only created when unset, the rejected promise is cached forever — every laterImageOptimizerCacheget/setthen awaits a rejected promise, so the image cache stays broken until the process restarts.How?
In
getOrInitDiskLRU(), guard the replay loop to skip entries withsize <= 0. Done at the singleton layer so any source of a 0-size entry is handled, and it respects the existingLRUCacheinvariant rather than weakening it.Verification
pnpm --filter=next buildpnpm --filter=next typesnode_modules/.bin/jest --config jest.config.js test/unit/image-optimizer/lru-disk-eviction.test.ts(8 passed, incl. newshould skip a 0-byte entry instead of poisoning the singleton)pnpm prettier --write+eslint --fixon changed files (clean)Fixes #93757