Skip to content

Commit

Permalink
fix: memory leak from warnOnce() (#73483)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
styfle authored Dec 4, 2024
1 parent 432d119 commit 0ea736e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/next/src/build/output/log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bold, green, magenta, red, yellow, white } from '../../lib/picocolors'
import { LRUCache } from '../../server/lib/lru-cache'

export const prefixes = {
wait: white(bold('○')),
Expand Down Expand Up @@ -76,11 +77,12 @@ export function trace(...message: any[]) {
prefixedLog('trace', ...message)
}

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

export function warnOnce(...message: any[]) {
const key = message.join(' ')
if (!warnOnceCache.has(key)) {
warnOnceCache.set(key)
warn(...message)
}
}

0 comments on commit 0ea736e

Please sign in to comment.