mongoose: Maximum call stack size exceeded

Do you want to request a feature or report a bug? bug

What is the current behavior? there are some other issues out there with maximum call stack size exceeded, but I#m unsure if they are related to this one:

[13.07.2021 11:15.30.785] [ERROR] RangeError: Maximum call stack size exceeded
--
  | 2021-07-13T11:15:30.785+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:34:5)
  | 2021-07-13T11:15:30.785+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.785+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.785+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.785+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)
  | 2021-07-13T11:15:30.785+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.785+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.785+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.785+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)
  | 2021-07-13T11:15:30.785+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.785+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.785+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.785+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.785+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.786+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)
  | 2021-07-13T11:15:30.786+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.786+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.786+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.786+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)
  | 2021-07-13T11:15:30.786+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.786+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.786+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.786+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)
  | 2021-07-13T11:15:30.786+02:00 | at nextFunction (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:867:5)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor._next (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:202:5)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:249:14
  | 2021-07-13T11:15:30.786+02:00 | at maybePromise (/app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:692:3)
  | 2021-07-13T11:15:30.786+02:00 | at Cursor.next (/app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:234:12)
  | 2021-07-13T11:15:30.786+02:00 | at Object._onNext (/app/node_modules/mongoose/lib/cursor/QueryCursor.js:422:21)
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/utils.js:704:5
  | 2021-07-13T11:15:30.786+02:00 | at /app/node_modules/mongoose/node_modules/mongodb/lib/cursor.js:252:9
  | 2021-07-13T11:15:30.786+02:00 | at handleCallback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/cursor.js:32:5)

If the current behavior is a bug, please provide the steps to reproduce. I cannot really reproduce it, as it seems it only happens from time to time, but it crashes the whole node process after that. I see following strange behaviours:

  1. JSON.stringify(mongooseModelResult[0].someProperty) returns e.g. { a: 1 }, but Object.values(mongooseModelResult[0].someProperty) returns something complete different? This reminds me of a memory issue in good old c? This issue is gone if I use .lean() for the query… still doesn’t seem right. The related has this structure:
user.privacy: {
		user_register: Date,
		user_application: Date,
		user_active_sourcing: Date,
		company_register: Date,
		company_posting: Date,
		company_application: Date,
		company_club: Date,
		company_digital_content: Date
	},

with one data point set:

user.privacy: {
user_register: some-Date
}

after the mongoose result, I add some more data to the object, in this case I added to the root object “user.mailBounces = {…}”. when I use Object.values of user.privacy I get also the mailBounces values returned? 2. For some models and queries it seems if I use populate the call stack size exceeded error appears. It only helps to remove populate from these kind of queries. THe strange thing is: it seems to be related with the data of the collecitno, as this is only happening for us on produciton, but not on the test environment? 3. The error above: The main issue with that is that I cannot even say where it is coming from, as the stacktrace doesn’t let me debug this. But I guess it has something similar like point 2.

{
	"compilerOptions": {
		// https://node.green/#ES2020
		"module": "commonjs",
		"target": "es2020",
		"lib": ["es2020"],

		// other best practice configs
		"noUncheckedIndexedAccess": true,
		"moduleResolution": "node",
		"strict": true,
		"noImplicitAny": false, 
		"removeComments": false,
		"emitDecoratorMetadata": true,
		"composite": true,
		"experimentalDecorators": true,
		"strictPropertyInitialization": false,
		"resolveJsonModule": true,
		"sourceMap": true,
		"isolatedModules": false,
		"declaration": true,
		"skipLibCheck": true,
		"noImplicitOverride": true
	},
	"files": ["types/lib.shim.d.ts", "types/mongoose.d.ts"],
	"exclude": ["node_modules", "**/__tests__"]
}

I put all these 3 issues into one ticket, as I hardly believe this is all related with virtuals somehow?

What is the expected behavior? no errors, I just recently upgraded from “mongoose”: “5.10.19”, to “mongoose”: “^5.13.2”, …there were no issues with 5.10.19.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. mongoose: 5.13.12 node: v16.4.0 mongodb: 4.4.1

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@simllll are you setting a batchSize on your cursors? Your stack trace makes it look like there’s a huge batchSize.

Thanks for pointing out the privacy.mailBounce issue, we fixed that in 62b7b9c

@simllll in v5.13.6 we’ll clear the stack every 1000 docs, so this stack overflow shouldn’t happen anymore. Let me know if this issue persists.

I can comment on this. The cursor doesn’t clean up after itself, so if you have millions of data points, it will eventually use up too much:

Screen Shot 2021-07-13 at 12 26 15

Here is a simplified version of my script:

const fs = require('fs')
const mongoose = require('mongoose')
const log = require('single-line-log').stdout

const PlacePointModel = require('./schema/PlacePoint.js').PlacePointModel

let total = 0

async function BUILD () {
  await mongoose.connect('mongodb://127.0.0.1:27017/data', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
  })

  const db = mongoose.connection

  const pointCursor = PlacePointModel.find().populate('relations').cursor()

  for (let point = await pointCursor.next(); point != null; point = await pointCursor.next()) {
    log(++total)
    // pull
    const { ... } = point
    // manipulate

    ...

    // store
    fs.appendFileSync('./data.json', JSON.stringify(feature) + '\n')
  }

  db.close()
}

BUILD()

version of node: 12.22.3 mongoose: 5.13.2