Skip to content

Commit

Permalink
perf: avoid leaking memory when using populate() with QueryCursor b…
Browse files Browse the repository at this point in the history
…ecause of reusing populate options with `_docs`

Fix #11641
  • Loading branch information
vkarpov15 committed May 6, 2022
1 parent 2091818 commit 55ff290
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4574,9 +4574,9 @@ function populate(model, docs, options, callback) {
}
// If no models to populate but we have a nested populate,
// keep trying, re: gh-8946
if (options.populate != null) {
const opts = utils.populate(options.populate).map(pop => Object.assign({}, pop, {
path: options.path + '.' + pop.path
if (populateOptions.populate != null) {
const opts = utils.populate(populateOptions.populate).map(pop => Object.assign({}, pop, {
path: populateOptions.path + '.' + pop.path
}));
return model.populate(docs, opts, callback);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ exports.populate = function populate(path, select, model, match, options, subPop
let obj = null;
if (arguments.length === 1) {
if (path instanceof PopulateOptions) {
// If reusing old populate docs, avoid reusing `_docs` because that may
// lead to bugs and memory leaks. See gh-11641
path._docs = [];
path._childDocs = [];
return [path];
}

Expand Down

0 comments on commit 55ff290

Please sign in to comment.