Skip to content

Commit

Permalink
lib: fix memory leak, when module 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 occuers
so that the memory can be garbage collected.

Fixes: #32836
  • Loading branch information
lianxuify committed Apr 15, 2020
1 parent 3f5142d commit d0b97d7
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 d0b97d7

Please sign in to comment.