mongoose: Add session idle timeout and description of "cursor not found" to docs about queries

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

What is the current behavior? I recently bumped my Mongoose version from 5.5.7 to 5.9.6 and I spotted a issue with cursors. The error is MongoError: cursor id XXX not found (even with cursor.addCursorFlag('noCursorTimeout', true).

If the current behavior is a bug, please provide the steps to reproduce. You can reproduce the bug by running this script (for a few hours) :

const mongoose = require('mongoose');

const sleep = seconds => new Promise(resolve => setTimeout(resolve, seconds * 1000));

(async () => {
	await mongoose.connect('mongodb://localhost/test', { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, poolSize: 10 });

	const Doc = mongoose.model('Test',  new mongoose.Schema({
		counter: { type: Number, default: 0 }
	}));

	for(let i = 0; i < 10000; ++i)
		await (new Doc()).save();

	const cursor = Doc.find({}).cursor();
	cursor.addCursorFlag('noCursorTimeout', true);
	await cursor.eachAsync(async doc => {
		doc.counter++;
		await doc.save();
		console.log(`Doc ${doc._id} saved, sleeping for 10 seconds...`);
		await sleep(10);
	});
	await cursor.close();
})();

What is the expected behavior? The error must not occur.

What are the versions of Node.js, Mongoose and MongoDB you are using? Node v12.16.1 Mongoose v5.9.6 MongoDB v4.0.16

I think the error has to do with the MongoDB idle session timeout (https://docs.mongodb.com/manual/reference/method/cursor.noCursorTimeout/) but I am no expert.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18

Most upvoted comments

Might be worth mentioning https://docs.mongodb.com/manual/reference/method/cursor.noCursorTimeout/#session-idle-timeout-overrides-nocursortimeout in the query docs. Admittedly keeping a cursor idle for 30 minutes is not a common use case, but something to make sure people are aware of.

Having to wait for a few hours to reproduce the issue, that’s my favorite kinds of issues 😄

I’ll run the script for 1,000 documents, it should take ~3 hours, hopefully that will be enough to reproduce the issue.