Skip to content

Model.find().cursor().destroy() does not close the underlying mongodb cursor #14966

@SleepWalker

Description

@SleepWalker

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

Last performant version

Slowed down in version

8.1.1

Node.js version

16.20.2

🦥 Performance issue

When you use streaming API of QueryCursor and you need to destory stream for some reason (e.g. the upstream was destoryed too) it won't close the cursor and the cursor and session will hang in memory (activeSessions)

Steps to Reproduce

const mongoose = require('mongoose');
const Model = require('./Model');

const stream = Model.find().cursor();

console.log(stream.cursor.closed);

stream.destroy();

console.log(stream.destroyed);
console.log(stream.cursor.closed);
console.log([...mongoose.connection.client.s.activeSessions.values()])

Expected Behavior

It looks to me that the correct behavior should be — to automatically call cursor.close() when the stream is destroyed

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions