Skip to content

Commit

Permalink
module: fix memory leak when require error occurs
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Andrey Pechkurov <[email protected]>
  • Loading branch information
lianxuify authored and MylesBorins committed Apr 17, 2020
1 parent 6681a68 commit fe1bda9
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
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,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);
}
}
}
} else if (module.exports &&
ObjectGetPrototypeOf(module.exports) ===
Expand Down
3 changes: 0 additions & 3 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
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 fe1bda9

Please sign in to comment.