Skip to content

Commit

Permalink
module: fix leak of vm.SyntheticModule
Browse files Browse the repository at this point in the history
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
Refs: #44211
Refs: #42080
Refs: #47096
Refs: #43205
Refs: #38695
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Stephen Belanger <[email protected]>
  • Loading branch information
joyeecheung authored and nodejs-github-bot committed Sep 14, 2023
1 parent bc9a875 commit 4aa6b8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ModuleWrap::ModuleWrap(Environment* env,
if (!synthetic_evaluation_step->IsUndefined()) {
synthetic_ = true;
}
MakeWeak();
}

ModuleWrap::~ModuleWrap() {
Expand Down
23 changes: 23 additions & 0 deletions test/es-module/test-vm-synthetic-module-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Flags: --experimental-vm-modules --max-old-space-size=16
'use strict';

// This tests that vm.SyntheticModule does not leak.
// See https://github.com/nodejs/node/issues/44211
require('../common');
const vm = require('vm');

let count = 0;
async function createModule() {
// Try to reach the maximum old space size.
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();

0 comments on commit 4aa6b8c

Please sign in to comment.