Files
bun-src/test/js/node/vm/sourcetextmodule-leak.test.ts
2026-08-27 21:09:14 +00:00

42 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const vm = require("vm");
const { describe, it, expect } = require("bun:test");
const { isASAN, isDebug, rss } = require("harness");
// 50k×50KB ≈ 2.5 GB of source text — if module records leak their source we
// blow past the threshold. Debug builds parse/link ~50× slower, so scale down.
// ASAN's quarantine raises the RSS floor; keep a wide-but-bounded ceiling there.
const ITERATIONS = isDebug ? 2_000 : 50_000;
const THRESHOLD_MB = isDebug ? (isASAN ? 1500 : 300) : isASAN ? 3500 : 3000;
describe("vm.SourceTextModule", () => {
it(
"shouldn't leak memory",
async () => {
const initialUsage = rss();
{
const source = `/*\n${Buffer.alloc(50_000, " * aaaaa\n").toString("utf8")}\n*/ Buffer.alloc(10, 'hello');`;
async function go(i) {
const mod = new vm.SourceTextModule(source + "//" + i, {
identifier: Buffer.alloc(64, i.toString()).toString("utf8"),
});
await mod.link(() => {});
await mod.evaluate();
}
for (let i = 0; i < ITERATIONS; ++i) {
await go(i);
}
}
Bun.gc(true);
const finalUsage = rss();
const megabytes = Math.round(((finalUsage - initialUsage) / 1024 / 1024) * 100) / 100;
expect(megabytes).toBeLessThan(THRESHOLD_MB);
},
isDebug ? 60_000 : 30_000,
);
});