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.
This commit is contained in:
Steven
2024-12-04 09:11:36 -05:00
committed by GitHub
parent 432d119ffc
commit 0ea736efc0
+6 -4
View File
@@ -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('○')),
@@ -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)
}
}