Fix memory leak in dev server

This commit is contained in:
2026-10-16 14:25:18 +01:00
parent 3d0771f2d0
commit 5dc783fe97
36 changed files with 642 additions and 406 deletions
+6
View File
@@ -0,0 +1,6 @@
/**
* Disposes the hydration and SSR esbuild contexts, releasing the incremental
* build state esbuild keeps in memory for them. The bundlers re-create the
* contexts on their next run.
*/
export default function disposeBundlerContexts(): Promise<void>;
+17
View File
@@ -0,0 +1,17 @@
/**
* Disposes the hydration and SSR esbuild contexts, releasing the incremental
* build state esbuild keeps in memory for them. The bundlers re-create the
* contexts on their next run.
*/
export default async function disposeBundlerContexts() {
try {
await global.BUNEXT_BUNDLER_CTX?.dispose();
}
catch (error) { }
global.BUNEXT_BUNDLER_CTX = undefined;
try {
await global.BUNEXT_SSR_BUNDLER_CTX?.dispose();
}
catch (error) { }
global.BUNEXT_SSR_BUNDLER_CTX = undefined;
}
+4
View File
@@ -8,6 +8,7 @@ import grabRootFilePath from "../server/web-pages/grab-root-file-path";
import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin";
import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker";
import cleanupArtifacts from "../server/cleanup-artifacts";
import { evictDeletedModules } from "../../utils/import-fresh";
import { writeFileSync } from "fs";
import path from "path";
import { log } from "../../utils/log";
@@ -165,6 +166,9 @@ async function doSSRRebuild(params) {
global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = false;
global.BUNEXT_BUILD_FAILED = false;
await cleanupArtifacts({ paradigm: "ssr" });
// esbuild has now replaced superseded outputs on disk; release the
// ones the server already imported.
evictDeletedModules(BUNX_CWD_MODULE_CACHE_DIR);
}
catch (error) {
global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = true;