Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor 'close' event emitted twice #10876

Closed
iovanom opened this issue Oct 13, 2021 · 5 comments · Fixed by #10878
Closed

Cursor 'close' event emitted twice #10876

iovanom opened this issue Oct 13, 2021 · 5 comments · Fixed by #10878
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@iovanom
Copy link
Contributor

iovanom commented Oct 13, 2021

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

What is the current behavior?
When the cursor is used with pause/resume the event 'close' is emitted twice.

If the current behavior is a bug, please provide the steps to reproduce.

cursor = TestModel.find({}).cursor();
cursor.on('data', (obj) => {
  cursor.pause();
  // simulate the async actions using obj
  setTimeout(() => cursor.resume(), 50);
});
cursor.on('close', () => console.log('close'));

The code below will get two 'close' in the console;

What is the expected behavior?

The expected behavior is to receive only one 'close' event.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node - 14.18.0
mongoose - 5.13.11
MongoDB - 4.4.9

@iovanom
Copy link
Contributor Author

iovanom commented Oct 13, 2021

I've found this PR https://github.com/Automattic/mongoose/pull/8834/files
I think, instead to user this solution with setTimeout should be used the _this.destroy() because the solution from PR fixed the issue only if the 'close' from autoDestroy was called first, instead if our _this.emit('close') was called first then the autoDestroy will emit 'close' one more time. With _this.destroy() this will be fixed.

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 13, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Oct 13, 2021

close only fires once for me

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);



async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();
    const cursor =  await Test.find({}).cursor();

    cursor.on('data', (obj) => {
      cursor.pause();
      // simulate the async actions using obj
      setTimeout(() => cursor.resume(), 50);
    });
    cursor.on('close', () => console.log('close'));
  
}


run();


@iovanom
Copy link
Contributor Author

iovanom commented Oct 14, 2021

close only fires once for me

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);



async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();
    const cursor =  await Test.find({}).cursor();

    cursor.on('data', (obj) => {
      cursor.pause();
      // simulate the async actions using obj
      setTimeout(() => cursor.resume(), 50);
    });
    cursor.on('close', () => console.log('close'));
  
}


run();

you don't have any document in collection, the pause/resume is not called if no documents. Have a look on my tests that I have added.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Oct 14, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

const Test = mongoose.model('Test', testSchema);



async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();
    await Test.create({name: 'Test'});
    await Test.create({name: 'Other Test'})
    const cursor =  await Test.find({}).cursor();

    cursor.on('data', (obj) => {
      cursor.pause();
      // simulate the async actions using obj
      setTimeout(() => cursor.resume(), 50);
    });
    cursor.on('close', () => console.log('close'));
  
}


run();

@iovanom
Copy link
Contributor Author

iovanom commented Oct 18, 2021

@vkarpov15 I've created a PR for version 5.x
#10897

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants