Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
module: fix memory leak when require error occurs
Delete useless module in parent module: parent.children array
when error occurs, so that it can be garbage collected.

Fixes: #32836

PR-URL: #32837
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
  • Loading branch information
lianxuify authored and targos committed Apr 28, 2020
1 parent bb1481f commit 6489a5b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -882,6 +882,13 @@ Module._load = function(request, parent, isMain) {
delete Module._cache[filename];
if (parent !== undefined) {
delete relativeResolveCache[relResolveCacheIdentifier];
const children = parent && parent.children;
if (ArrayIsArray(children)) {
const index = children.indexOf(module);
if (index !== -1) {
children.splice(index, 1);
}
}
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/sequential/test-module-loading.js
Expand Up @@ -302,17 +302,14 @@ assert.throws(
}
},
'fixtures/path.js': {},
'fixtures/throws_error.js': {},
'fixtures/registerExt.test': {},
'fixtures/registerExt.hello.world': {},
'fixtures/registerExt2.test': {},
'fixtures/module-load-order/file1': {},
'fixtures/module-load-order/file2.js': {},
'fixtures/module-load-order/file3.node': {},
'fixtures/module-load-order/file4.reg': {},
'fixtures/module-load-order/file5.reg2': {},
'fixtures/module-load-order/file6/index.js': {},
'fixtures/module-load-order/file7/index.node': {},
'fixtures/module-load-order/file8/index.reg': {},
'fixtures/module-load-order/file9/index.reg2': {},
'fixtures/module-require/parent/index.js': {
Expand Down

0 comments on commit 6489a5b

Please sign in to comment.