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

Leaking sessions #11256

Closed
morkai opened this issue Jan 23, 2022 · 1 comment
Closed

Leaking sessions #11256

morkai opened this issue Jan 23, 2022 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@morkai
Copy link

morkai commented Jan 23, 2022

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

What is the current behavior?
MongoDB sessions created by using mongoose.connection.transaction() aren't cleaned up, resulting in the memory leaking:

image

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

const mongoose = require('mongoose');

mongoose.model('Test', new mongoose.Schema({}));

async function main()
{
  await mongoose.connect('mongodb://127.0.0.1:27017/test');
  
  setTimeout(leak, 1000);
}

async function leak()
{
  await mongoose.connection.transaction(async session =>
  {
    await mongoose.model('Test').findOne().session(session).exec();
  });
  
  console.log(
    new Date().toISOString(),
    'sessions =',
    mongoose.connection.client.topology.s.sessions.size
  );
  
  setTimeout(leak, 1000);
}

main();
$ node leak.js
2022-01-23T17:14:16.521Z sessions = 1
2022-01-23T17:14:17.542Z sessions = 2
2022-01-23T17:14:18.555Z sessions = 3
2022-01-23T17:14:19.563Z sessions = 4
2022-01-23T17:14:20.580Z sessions = 5
2022-01-23T17:14:21.603Z sessions = 6
2022-01-23T17:14:22.621Z sessions = 7
2022-01-23T17:14:23.631Z sessions = 8
2022-01-23T17:14:24.645Z sessions = 9
2022-01-23T17:14:25.662Z sessions = 10
...

What is the expected behavior?
mongoose.connection.transaction() should call session.endSession() so the session is cleaned up.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node v16.13.2
mongoose v6.1.7
mongodb server v5.0.5 driver v4.3.0

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jan 24, 2022

Created a PR #11259

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 24, 2022
@vkarpov15 vkarpov15 added this to the 6.1.8 milestone Jan 24, 2022
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

No branches or pull requests

4 participants