Skip to content

Commit

Permalink
perf(cursor): clear the stack every time if using populate with batch…
Browse files Browse the repository at this point in the history
…Size to avoid stack overflows with large docs
  • Loading branch information
vkarpov15 committed Dec 27, 2024
1 parent 4081e5c commit 5419077
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 5419077

Please sign in to comment.