Skip to content

Commit 2e05cf1

Browse files
joyeecheungrichardlau
authored andcommitted
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. PR-URL: #48510 Backport-PR-URL: #51004 Refs: #44211 Refs: #42080 Refs: #47096 Refs: #43205 Refs: #38695 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent a86a2e1 commit 2e05cf1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/module_wrap.cc

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ ModuleWrap::ModuleWrap(Environment* env,
6464
if (!synthetic_evaluation_step->IsUndefined()) {
6565
synthetic_ = true;
6666
}
67+
MakeWeak();
6768
}
6869

6970
ModuleWrap::~ModuleWrap() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --experimental-vm-modules --max-old-space-size=16
2+
'use strict';
3+
4+
// This tests that vm.SyntheticModule does not leak.
5+
// See https://github.com/nodejs/node/issues/44211
6+
require('../common');
7+
const vm = require('vm');
8+
9+
let count = 0;
10+
async function createModule() {
11+
// Try to reach the maximum old space size.
12+
const m = new vm.SyntheticModule(['bar'], () => {
13+
m.setExport('bar', new Array(512).fill('----'));
14+
});
15+
await m.link(() => {});
16+
await m.evaluate();
17+
if (count++ < 4 * 1024) {
18+
setTimeout(createModule, 1);
19+
}
20+
return m;
21+
}
22+
23+
createModule();

0 commit comments

Comments
 (0)