Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add and use checkIfCollectable in vm leak tests #49671

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/common/gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

// TODO(joyeecheung): merge ongc.js and gcUntil from common/index.js
// into this.

// This function can be used to check if an object factor leaks or not,
// but it needs to be used with care:
// 1. The test should be set up with an ideally small
// --max-old-space-size or --max-heap-size, which combined with
// the maxCount parameter can reproduce a leak of the objects
// created by fn().
// 2. This works under the assumption that if *none* of the objects
// created by fn() can be garbage-collected, the test would crash due
// to OOM.
// 3. If *any* of the objects created by fn() can be garbage-collected,
// it is considered leak-free. The FinalizationRegistry is used to
// terminate the test early once we detect any of the object is
// garbage-collected to make the test less prone to false positives.
// This may be especially important for memory management relying on
// emphemeron GC which can be inefficient to deal with extremely fast
// heap growth.
// Note that this can still produce false positives. When the test using
// this function still crashes due to OOM, inspect the heap to confirm
// if a leak is present (e.g. using heap snapshots).
async function checkIfCollectable(fn, maxCount = 4096, logEvery = 128) {
let anyFinalized = false;
let count = 0;

const f = new FinalizationRegistry(() => {
anyFinalized = true;
});

async function createObject() {
const obj = await fn();
f.register(obj);
if (count % logEvery === 1) {
console.log('Created', count);
}
if (count++ < maxCount && !anyFinalized) {
setImmediate(createObject);
}
}

createObject();
}

module.exports = {
checkIfCollectable,
};
13 changes: 5 additions & 8 deletions test/es-module/test-vm-compile-function-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
// This tests that vm.compileFunction with dynamic import callback does not leak.
// See https://github.com/nodejs/node/issues/44211
require('../common');
const { checkIfCollectable } = require('../common/gc');
const vm = require('vm');
let count = 0;

function main() {
// Try to reach the maximum old space size.
vm.compileFunction(`"${Math.random().toString().repeat(512)}"`, [], {
async function createCompiledFunction() {
return vm.compileFunction(`"${Math.random().toString().repeat(512)}"`, [], {
async importModuleDynamically() {},
});
if (count++ < 2048) {
setTimeout(main, 1);
}
}
main();

checkIfCollectable(createCompiledFunction, 2048);
11 changes: 4 additions & 7 deletions test/es-module/test-vm-contextified-script-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
// This tests that vm.Script with dynamic import callback does not leak.
// See: https://github.com/nodejs/node/issues/33439
require('../common');
const { checkIfCollectable } = require('../common/gc');
const vm = require('vm');
let count = 0;

function main() {
async function createContextifyScript() {
// Try to reach the maximum old space size.
new vm.Script(`"${Math.random().toString().repeat(512)}";`, {
return new vm.Script(`"${Math.random().toString().repeat(512)}";`, {
async importModuleDynamically() {},
});
if (count++ < 2 * 1024) {
setTimeout(main, 1);
}
}
main();
checkIfCollectable(createContextifyScript, 2048);
18 changes: 8 additions & 10 deletions test/es-module/test-vm-source-text-module-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
// This tests that vm.SourceTextModule() does not leak.
// See: https://github.com/nodejs/node/issues/33439
require('../common');

const { checkIfCollectable } = require('../common/gc');
const vm = require('vm');
let count = 0;
async function createModule() {

async function createSourceTextModule() {
// Try to reach the maximum old space size.
const m = new vm.SourceTextModule(`
const bar = new Array(512).fill("----");
export { bar };
`);
const bar = new Array(512).fill("----");
export { bar };
`);
await m.link(() => {});
await m.evaluate();
if (count++ < 4096) {
setTimeout(createModule, 1);
}
return m;
}
createModule();

checkIfCollectable(createSourceTextModule, 4096);
11 changes: 3 additions & 8 deletions test/es-module/test-vm-synthetic-module-leak.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
// This tests that vm.SyntheticModule does not leak.
// See https://github.com/nodejs/node/issues/44211
require('../common');
const { checkIfCollectable } = require('../common/gc');
const vm = require('vm');

let count = 0;
async function createModule() {
// Try to reach the maximum old space size.
async function createSyntheticModule() {
const m = new vm.SyntheticModule(['bar'], () => {
m.setExport('bar', new Array(512).fill('----'));
});
await m.link(() => {});
await m.evaluate();
if (count++ < 4 * 1024) {
setTimeout(createModule, 1);
}
return m;
}

createModule();
checkIfCollectable(createSyntheticModule, 4096);