Skip to content

Commit

Permalink
fix(cursor): make sure to clear stack every 1000 docs when calling `n…
Browse files Browse the repository at this point in the history
…ext()` to avoid stack overflow with large batch size

Fix #10449
  • Loading branch information
vkarpov15 committed Aug 9, 2021
1 parent 95685e2 commit a0bdcfe
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/cursor/QueryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ function _onNext(error, doc) {
this.ctx._batchDocs.push(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(_onNext.bind(this)));
}
this.ctx.cursor.next(_onNext.bind(this));
} else {
_populateBatch.call(this);
Expand Down

0 comments on commit a0bdcfe

Please sign in to comment.