Fix memory leak in dev server
This commit is contained in:
Vendored
+21
-4
@@ -1,7 +1,24 @@
|
||||
/**
|
||||
* Dev: bypass Bun's module registry so rebuilt files are picked up.
|
||||
* Prod: import by stable path so each module is loaded (and kept) once.
|
||||
* A unique `?t=` per call creates a new, never-evicted module record, so
|
||||
* using it in production leaks memory on every request.
|
||||
* Imports a module by its stable path.
|
||||
*
|
||||
* Dev: when the file changed since it was last imported, its registry
|
||||
* entry is evicted first so the new version is evaluated and the old one
|
||||
* can be garbage collected. A cache-busting `?t=` query would instead
|
||||
* create a new, never-evicted module record on every call.
|
||||
*
|
||||
* Prod: plain import, so each module is loaded (and kept) once.
|
||||
*/
|
||||
export default function importFresh<T = any>(file_path: string): Promise<T>;
|
||||
/**
|
||||
* Drops a module from Bun's registry so its memory can be reclaimed and the
|
||||
* next import re-evaluates it. Only the module itself is evicted, not the
|
||||
* modules it imports.
|
||||
*/
|
||||
export declare function evictModule(file_path: string): void;
|
||||
/**
|
||||
* Evicts every registry entry under `dir` whose file no longer exists. Bundle
|
||||
* outputs (entries and shared chunks) are content-hashed and esbuild deletes
|
||||
* the superseded ones on rebuild, so a missing file means no current module
|
||||
* imports it anymore.
|
||||
*/
|
||||
export declare function evictDeletedModules(dir: string): void;
|
||||
|
||||
Vendored
+61
-6
@@ -1,10 +1,65 @@
|
||||
import { existsSync, statSync } from "fs";
|
||||
import path from "path";
|
||||
import isDevelopment from "./is-development";
|
||||
/**
|
||||
* Dev: bypass Bun's module registry so rebuilt files are picked up.
|
||||
* Prod: import by stable path so each module is loaded (and kept) once.
|
||||
* A unique `?t=` per call creates a new, never-evicted module record, so
|
||||
* using it in production leaks memory on every request.
|
||||
* File path -> mtime of the version currently held in Bun's module
|
||||
* registry (dev only).
|
||||
*/
|
||||
export default function importFresh(file_path) {
|
||||
return import(isDevelopment() ? `${file_path}?t=${Date.now()}` : file_path);
|
||||
const loaded_versions = new Map();
|
||||
/**
|
||||
* Imports a module by its stable path.
|
||||
*
|
||||
* Dev: when the file changed since it was last imported, its registry
|
||||
* entry is evicted first so the new version is evaluated and the old one
|
||||
* can be garbage collected. A cache-busting `?t=` query would instead
|
||||
* create a new, never-evicted module record on every call.
|
||||
*
|
||||
* Prod: plain import, so each module is loaded (and kept) once.
|
||||
*/
|
||||
export default async function importFresh(file_path) {
|
||||
if (!isDevelopment()) {
|
||||
return import(file_path);
|
||||
}
|
||||
let mtime;
|
||||
try {
|
||||
mtime = statSync(file_path).mtimeMs;
|
||||
}
|
||||
catch { }
|
||||
if (loaded_versions.has(file_path) &&
|
||||
loaded_versions.get(file_path) !== mtime) {
|
||||
evictModule(file_path);
|
||||
}
|
||||
try {
|
||||
const mod = await import(file_path);
|
||||
loaded_versions.set(file_path, mtime);
|
||||
return mod;
|
||||
}
|
||||
catch (error) {
|
||||
// Don't keep a failed evaluation around: the next request retries.
|
||||
evictModule(file_path);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Drops a module from Bun's registry so its memory can be reclaimed and the
|
||||
* next import re-evaluates it. Only the module itself is evicted, not the
|
||||
* modules it imports.
|
||||
*/
|
||||
export function evictModule(file_path) {
|
||||
delete require.cache[file_path];
|
||||
loaded_versions.delete(file_path);
|
||||
}
|
||||
/**
|
||||
* Evicts every registry entry under `dir` whose file no longer exists. Bundle
|
||||
* outputs (entries and shared chunks) are content-hashed and esbuild deletes
|
||||
* the superseded ones on rebuild, so a missing file means no current module
|
||||
* imports it anymore.
|
||||
*/
|
||||
export function evictDeletedModules(dir) {
|
||||
const prefix = dir.endsWith(path.sep) ? dir : dir + path.sep;
|
||||
for (const key of Object.keys(require.cache)) {
|
||||
if (key.startsWith(prefix) && !existsSync(key)) {
|
||||
evictModule(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user