Unverified Commit 9e5759de authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

fix: ensure LRU cache items have minimum size of 1 to prevent unbounded growth (#89040)

### What?

Prevent LRU cache unbounded growth by requiring `calculateSize` to
return values > 0.

### Why?

When `calculateSize` returned 0 (e.g., for `null` values in the
filesystem route cache), items were never evicted because they didn't
contribute to `totalSize`. This caused unbounded memory growth when
handling dynamic routes like `/api/logs/[id]` where each unique ID
created a new cache entry with size 0.

### How?

1. **LRUCache now throws an error if size ≤ 0** - This catches bugs at
development time rather than silently masking them:
   ```typescript
   if (size <= 0) {
     throw new Error(
\`LRUCache: calculateSize returned ${size}, but size must be > 0. \` +
\`Items with size 0 would never be evicted, causing unbounded cache
growth.\`
     )
   }
   ```

2. **Fixed filesystem.ts** - Null entries (negative cache) now return
size 1 instead of 0:
   ```typescript
   if (!value) {
// Null entries (negative cache) still need a non-zero size for LRU
eviction
     return 1
   }
   ```

3. **Fixed next-dev-server.ts** - Static paths cache now uses `|| 1`
instead of `?? 0`:
   ```typescript
   return JSON.stringify(value.staticPaths)?.length || 1
   ```
parent b690af09
...@@ -981,5 +981,6 @@ ...@@ -981,5 +981,6 @@
"980": "Failed to load client middleware manifest", "980": "Failed to load client middleware manifest",
"981": "resolvedPathname must be set in request metadata", "981": "resolvedPathname must be set in request metadata",
"982": "`serializeResumeDataCache` should not be called in edge runtime.", "982": "`serializeResumeDataCache` should not be called in edge runtime.",
"983": "Invariant: global-error module is required but not found in loader tree" "983": "Invariant: global-error module is required but not found in loader tree",
"984": "LRUCache: calculateSize returned %s, but size must be > 0. Items with size 0 would never be evicted, causing unbounded cache growth."
} }
...@@ -186,7 +186,8 @@ export default class DevServer extends Server { ...@@ -186,7 +186,8 @@ export default class DevServer extends Server {
// 5MB // 5MB
5 * 1024 * 1024, 5 * 1024 * 1024,
function length(value) { function length(value) {
return JSON.stringify(value.staticPaths)?.length ?? 0 // Ensure minimum size of 1 for LRU eviction to work correctly
return JSON.stringify(value.staticPaths)?.length || 1
} }
) )
......
...@@ -125,6 +125,12 @@ export class LRUCache<T> { ...@@ -125,6 +125,12 @@ export class LRUCache<T> {
*/ */
public set(key: string, value: T): void { public set(key: string, value: T): void {
const size = this.calculateSize?.(value) ?? 1 const size = this.calculateSize?.(value) ?? 1
if (size <= 0) {
throw new Error(
`LRUCache: calculateSize returned ${size}, but size must be > 0. ` +
`Items with size 0 would never be evicted, causing unbounded cache growth.`
)
}
if (size > this.maxSize) { if (size > this.maxSize) {
console.warn('Single item size exceeds maxSize') console.warn('Single item size exceeds maxSize')
return return
......
...@@ -113,7 +113,10 @@ export async function setupFsCheck(opts: { ...@@ -113,7 +113,10 @@ export async function setupFsCheck(opts: {
}) { }) {
const getItemsLru = !opts.dev const getItemsLru = !opts.dev
? new LRUCache<FsOutput | null>(1024 * 1024, function length(value) { ? new LRUCache<FsOutput | null>(1024 * 1024, function length(value) {
if (!value) return 0 if (!value) {
// Null entries (negative cache) still need a non-zero size for LRU eviction
return 1
}
return ( return (
(value.fsPath || '').length + (value.fsPath || '').length +
value.itemPath.length + value.itemPath.length +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment