Unverified Commit 72085357 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

perf: fix memory leaks in the edge runtime in dev (#57235)

This PR fixes a memory leak when using `next dev` where on HMR, we would always retain the memory associated with the old VM instance, leading to pretty egregious usage of RAM after only a few edits.

The leak itself comes from methods like `setTimeout` and `setInterval` capturing the context in which they were called and somehow never releasing, even if the timeout happened, when running in a Node.js `vm`. This is probably a Node.js bug.

The fix consists of taking ownership of all timeouts and clearing them ourselves manually at the end of each VM lifecycle.

Tested manually:
- hello world Next.js app with edge runtime
- triggered hmr 10 times
- memory usage
  - base: 400MB
  -  before: 800MB
  - after: 400MB

parent eb4b41e5
...@@ -24,6 +24,7 @@ import EventsImplementation from 'node:events' ...@@ -24,6 +24,7 @@ import EventsImplementation from 'node:events'
import AssertImplementation from 'node:assert' import AssertImplementation from 'node:assert'
import UtilImplementation from 'node:util' import UtilImplementation from 'node:util'
import AsyncHooksImplementation from 'node:async_hooks' import AsyncHooksImplementation from 'node:async_hooks'
import { intervalsManager, timeoutsManager } from './resource-managers'
interface ModuleContext { interface ModuleContext {
runtime: EdgeRuntime runtime: EdgeRuntime
...@@ -44,8 +45,14 @@ const pendingModuleCaches = new Map<string, Promise<ModuleContext>>() ...@@ -44,8 +45,14 @@ const pendingModuleCaches = new Map<string, Promise<ModuleContext>>()
* For a given path a context, this function checks if there is any module * For a given path a context, this function checks if there is any module
* context that contains the path with an older content and, if that's the * context that contains the path with an older content and, if that's the
* case, removes the context from the cache. * case, removes the context from the cache.
*
* This function also clears all intervals and timeouts created by the
* module context.
*/ */
export async function clearModuleContext(path: string) { export async function clearModuleContext(path: string) {
intervalsManager.removeAll()
timeoutsManager.removeAll()
const handleContext = ( const handleContext = (
key: string, key: string,
cache: ReturnType<(typeof moduleContexts)['get']>, cache: ReturnType<(typeof moduleContexts)['get']>,
...@@ -378,6 +385,22 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), ...@@ -378,6 +385,22 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
context.AsyncLocalStorage = AsyncLocalStorage context.AsyncLocalStorage = AsyncLocalStorage
// @ts-ignore the timeouts have weird types in the edge runtime
context.setInterval = (...args: Parameters<typeof setInterval>) =>
intervalsManager.add(args)
// @ts-ignore the timeouts have weird types in the edge runtime
context.clearInterval = (interval: number) =>
intervalsManager.remove(interval)
// @ts-ignore the timeouts have weird types in the edge runtime
context.setTimeout = (...args: Parameters<typeof setTimeout>) =>
timeoutsManager.add(args)
// @ts-ignore the timeouts have weird types in the edge runtime
context.clearTimeout = (timeout: number) =>
timeoutsManager.remove(timeout)
return context return context
}, },
}) })
......
abstract class ResourceManager<T, K> {
private resources: T[] = []
abstract create(resourceArgs: K): T
abstract destroy(resource: T): void
add(resourceArgs: K) {
const resource = this.create(resourceArgs)
this.resources.push(resource)
return resource
}
remove(resource: T) {
this.resources = this.resources.filter((r) => r !== resource)
this.destroy(resource)
}
removeAll() {
this.resources.forEach(this.destroy)
this.resources = []
}
}
class IntervalsManager extends ResourceManager<
number,
Parameters<typeof setInterval>
> {
create(args: Parameters<typeof setInterval>) {
// TODO: use the edge runtime provided `setInterval` instead
return setInterval(...args)[Symbol.toPrimitive]()
}
destroy(interval: number) {
clearInterval(interval)
}
}
class TimeoutsManager extends ResourceManager<
number,
Parameters<typeof setTimeout>
> {
create(args: Parameters<typeof setTimeout>) {
// TODO: use the edge runtime provided `setTimeout` instead
return setTimeout(...args)[Symbol.toPrimitive]()
}
destroy(timeout: number) {
clearTimeout(timeout)
}
}
export const intervalsManager = new IntervalsManager()
export const timeoutsManager = new TimeoutsManager()
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