Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
lib: fix memory leak, when module require error occurs
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
commit d0b97d780d6fd481cc449a03ff788856593fa74c
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': {},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit test for module loading, verify module.children contains all the different modules that we've required, but should not contain modules that failed to load

'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