Skip to content

Commit 0ea736e

Browse files
authored
fix: memory leak from warnOnce() (#73483)
The `warnOnce()` function is used to warn only once time, however this means we might be storing every possible message in memory forever (aka memory leak) since it can grow unbounded. This PR changes the behavior to only store only 10KB in a Least Recently Used (LRU) cache. The tradeoff here is that you might see the same warning twice if you have a lot of unique warnings.
1 parent 432d119 commit 0ea736e

File tree

1 file changed

+6
-4
lines changed
  • packages/next/src/build/output

1 file changed

+6
-4
lines changed

packages/next/src/build/output/log.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { bold, green, magenta, red, yellow, white } from '../../lib/picocolors'
2+
import { LRUCache } from '../../server/lib/lru-cache'
23

34
export const prefixes = {
45
wait: white(bold('○')),
@@ -76,11 +77,12 @@ export function trace(...message: any[]) {
7677
prefixedLog('trace', ...message)
7778
}
7879

79-
const warnOnceMessages = new Set()
80-
export function warnOnce(...message: any[]) {
81-
if (!warnOnceMessages.has(message[0])) {
82-
warnOnceMessages.add(message.join(' '))
80+
const warnOnceCache = new LRUCache<string>(10_000, (message) => message.length)
8381

82+
export function warnOnce(...message: any[]) {
83+
const key = message.join(' ')
84+
if (!warnOnceCache.has(key)) {
85+
warnOnceCache.set(key)
8486
warn(...message)
8587
}
8688
}

0 commit comments

Comments
 (0)