Skip to content

Commit

Permalink
Merge pull request #15136 from Automattic/vkarpov15/gh-10449-2
Browse files Browse the repository at this point in the history
perf(cursor): clear the stack every time if using populate with batchSize to avoid stack overflows with large docs
  • Loading branch information
vkarpov15 authored Dec 30, 2024
2 parents 494df86 + 5419077 commit ddd95b8
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,11 @@ function _onNext(error, doc) {

if (this.ctx._batchDocs.length < this.ctx.options._populateBatchSize) {
// If both `batchSize` and `_populateBatchSize` are huge, calling `next()` repeatedly may
// cause a stack overflow. So make sure we clear the stack regularly.
if (this.ctx._batchDocs.length > 0 && this.ctx._batchDocs.length % 1000 === 0) {
return immediate(() => this.ctx.cursor.next().then(
res => { _onNext.call(this, null, res); },
err => { _onNext.call(this, err); }
));
}
this.ctx.cursor.next().then(
// cause a stack overflow. So make sure we clear the stack.
immediate(() => this.ctx.cursor.next().then(
res => { _onNext.call(this, null, res); },
err => { _onNext.call(this, err); }
);
));
} else {
_populateBatch.call(this);
}
Expand Down

0 comments on commit ddd95b8

Please sign in to comment.