diff options
author | Qinhui Chen <lianxuify@gmail.com> | 2020-04-14 05:58:01 +0300 |
---|---|---|
committer | Andrey Pechkurov <apechkurov@gmail.com> | 2020-04-17 12:35:00 +0300 |
commit | 6bcf96840121f2de61def5ad99700fb7eb6ac905 (patch) | |
tree | 671d9d71f71067554a6ef0ce930725d0333ed6ec /lib/internal/modules | |
parent | 88560ce593f87da3e869813b98edad3330ddac72 (diff) |
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: https://github.com/nodejs/node/issues/32836
PR-URL: https://github.com/nodejs/node/pull/32837
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Diffstat (limited to 'lib/internal/modules')
-rw-r--r-- | lib/internal/modules/cjs/loader.js | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 96aa4e2ebfc..9db2b1e6c1c 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -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) === |