Files
bun.sh/test/js/node/worker_threads/eval-source-leak-fixture.js
190n eee5d4fb4a node:worker_threads low-hanging fruit (#18758)
Co-authored-by: 190n <7763597+190n@users.noreply.github.com>
Co-authored-by: Ashcon Partovi <ashcon@partovi.net>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
Co-authored-by: Don Isaac <donald.isaac@gmail.com>
Co-authored-by: chloe caruso <git@paperclover.net>
2025-04-08 05:29:53 -07:00

41 lines
1.3 KiB
JavaScript
Generated

// Create a worker with extremely large source code which completes instantly and the `eval` option
// set to true. Ensure that the Blob created to hold the source code is not kept in memory after the
// worker exits.
const { Worker } = require("node:worker_threads");
const eachSizeMiB = 100;
const iterations = 5;
function test() {
const code = " ".repeat(eachSizeMiB * 1024 * 1024);
return new Promise((resolve, reject) => {
const worker = new Worker(code, { eval: true });
worker.on("exit", () => resolve());
worker.on("error", e => reject(e));
});
}
async function reallyGC() {
for (let i = 0; i < 3; i++) {
await Bun.sleep(5);
Bun.gc(true);
}
}
// warmup
await test();
await reallyGC();
const before = process.memoryUsage.rss();
for (let i = 0; i < iterations; i++) {
await test();
await reallyGC();
}
const after = process.memoryUsage.rss();
// The bug is that the source code passed to `new Worker` would never be freed.
// If this bug is present, then the memory growth likely won't be much more than the total amount
// of source code, but it's impossible for the memory growth to be less than the source code size.
// On macOS before fixing this bug, deltaMiB was around 503.
const deltaMiB = (after - before) / 1024 / 1024;
if (deltaMiB >= eachSizeMiB * iterations) throw new Error(`leaked ${deltaMiB} MiB`);