Skip to content

Commit 1751348

Browse files
committed
module: fix leak of vm.SyntheticModule
Previously we maintain a strong persistent reference to the ModuleWrap to retrieve the ID-to-ModuleWrap mapping from the HostImportModuleDynamicallyCallback using the number ID stored in the host-defined options. As a result the ModuleWrap would be kept alive until the Environment is shut down, which would be a leak for user code. With the new symbol-based host-defined option we can just get the ModuleWrap from the JS-land WeakMap so there's now no need to maintain this strong reference. This would at least fix the leak for vm.SyntheticModule. vm.SourceTextModule is still leaking due to the strong persistent reference to the v8::Module.
1 parent aa328cb commit 1751348

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/module_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ ModuleWrap::ModuleWrap(Environment* env,
5858
object->SetInternalField(kURLSlot, url);
5959
object->SetInternalField(kSyntheticEvaluationStepsSlot, undefined);
6060
object->SetInternalField(kContextObjectSlot, undefined);
61+
MakeWeak();
6162
}
6263

6364
ModuleWrap::~ModuleWrap() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// Flags: --experimental-vm-modules --max-heap-size=20
4+
// This tests that vm.SyntheticModule does not leak.
5+
// See https://github.com/nodejs/node/issues/44211
6+
7+
require('../common');
8+
const vm = require('vm');
9+
10+
let count = 0;
11+
async function createModule() {
12+
const m = new vm.SyntheticModule(['bar'], () => {
13+
m.setExport('bar', new Date().toISOString().repeat(1e6));
14+
});
15+
await m.link(() => {});
16+
await m.evaluate();
17+
count++;
18+
if (count < 30000) {
19+
setImmediate(createModule);
20+
}
21+
return m;
22+
}
23+
24+
createModule();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
// Flags: --experimental-vm-modules --max-heap-size=20
4+
// This tests that using vm.compileFunction() with vm.SyntheticModule in
5+
// the dynamic import callback does not leak.
6+
// See https://github.com/nodejs/node/issues/44211
7+
8+
require('../common');
9+
const vm = require('vm');
10+
11+
let count = 0;
12+
async function createModule() {
13+
const m = new vm.SourceTextModule('export default "hello".repeat(1e5)');
14+
await m.link(() => {});
15+
await m.evaluate();
16+
count++;
17+
if (count < 30000) {
18+
setImmediate(createModule);
19+
}
20+
return m;
21+
}
22+
23+
createModule();

0 commit comments

Comments
 (0)