Skip to content

fix(image): skip 0-byte entries when initializing disk LRU cache#94068

Draft
hammadxcm wants to merge 1 commit into
vercel:canaryfrom
hammadxcm:fix/image-disk-lru-zero-byte
Draft

fix(image): skip 0-byte entries when initializing disk LRU cache#94068
hammadxcm wants to merge 1 commit into
vercel:canaryfrom
hammadxcm:fix/image-disk-lru-zero-byte

Conversation

@hammadxcm
Copy link
Copy Markdown

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 a size: 0 entry. During init, getOrInitDiskLRU() replays entries via lru.set(entry.key, entry.size), but LRUCache.set() intentionally throws when size <= 0 (guard against unbounded growth). That throw rejects the singleton IIFE, and because _diskLRUPromise is only created when unset, the rejected promise is cached forever — every later ImageOptimizerCache get/set then awaits a rejected promise, so the image cache stays broken until the process restarts.

How?

In getOrInitDiskLRU(), guard the replay loop to skip entries with size <= 0. Done at the singleton layer so any source of a 0-size entry is handled, and it respects the existing LRUCache invariant rather than weakening it.

Verification

  • pnpm --filter=next build
  • pnpm --filter=next types
  • node_modules/.bin/jest --config jest.config.js test/unit/image-optimizer/lru-disk-eviction.test.ts (8 passed, incl. new should skip a 0-byte entry instead of poisoning the singleton)
  • pnpm prettier --write + eslint --fix on changed files (clean)

Fixes #93757

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.
@hammadxcm hammadxcm force-pushed the fix/image-disk-lru-zero-byte branch from 4c32eb1 to 640a483 Compare May 23, 2026 06:41
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.

next/image: a single 0-byte file in .next/cache/images/ permanently poisons the disk-LRU singleton

1 participant