Unverified Commit a2e29009 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[backport]: fix: ensure LRU cache items have minimum size of 1 to prevent unbounded growth (#89134)

Backports:
- #89040
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent a2b10ab0
...@@ -786,5 +786,6 @@ ...@@ -786,5 +786,6 @@
"785": "Request body exceeded %s", "785": "Request body exceeded %s",
"786": "Server Actions are not enabled for this application. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action", "786": "Server Actions are not enabled for this application. This request might be from an older or newer deployment.\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"787": "Failed to find Server Action. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action", "787": "Failed to find Server Action. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"788": "Failed to find Server Action%s. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action" "788": "Failed to find Server Action%s. This request might be from an older or newer deployment.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"789": "LRUCache: calculateSize returned %s, but size must be > 0. Items with size 0 would never be evicted, causing unbounded cache growth."
} }
...@@ -175,7 +175,8 @@ export default class DevServer extends Server { ...@@ -175,7 +175,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
} }
) )
this.renderOpts.ampSkipValidation = this.renderOpts.ampSkipValidation =
......
...@@ -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
......
...@@ -110,7 +110,10 @@ export async function setupFsCheck(opts: { ...@@ -110,7 +110,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