Skip to content

Connection count not responding to maxIdleTimeMS #15362

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

Open
1 task done
coleweinman opened this issue Apr 19, 2025 · 3 comments
Open
1 task done

Connection count not responding to maxIdleTimeMS #15362

coleweinman opened this issue Apr 19, 2025 · 3 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@coleweinman
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.13.2

Node.js version

18

MongoDB version

8.0.8

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I'm debugging an issue where my connection count increases over time like a memory leak. I'm using maxIdleTimeMS but after some testing it's not working the way I understand it should.

When I create a connection and run a query my connection count increases by a few (21 -> 26). Now, after maxIdleTimeMS has gone by, I continue to get the same count (26).

However, if I initiate another query on this connection, all of a sudden my connections drop (24). If I don't specify maxIdleTimeMS, the connection count remains the same (26).

At first I thought maybe I needed to adjust something like heartbeatFrequencyMS but that didn't change anything.

It looks like the "idle" connections aren't identifying themselves until a subsequent request is made to the database. Is this expected? Am I missing something? My issue is I need the connections to close automatically without subsequent requests.

const testC = await mongoose.connect(
      'mongodb+srv://....',
      {
        maxIdleTimeMS: 3000,
        heartbeatFrequencyMS: 3000,
        socketTimeoutMS: 3000,
      },
    );
    // 21
    const Question = testC.model('Question', QuestionSchema);
    await Question.find({});
    // 26
    await new Promise((resolve) => setTimeout(resolve, 5000));
    await Question.find({});
    // 24
    await testC.disconnect();
    // 21
@coleweinman coleweinman added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Apr 19, 2025
@coleweinman
Copy link
Author

Upon further testing I noticed that if I added minPoolSize it will work like I expected. Is this correct? Is there a way it can scale down to zero?

@vkarpov15
Copy link
Collaborator

This all appears to be expected with how the MongoDB Node driver works internally here: https://github.com/mongodb/node-mongodb-native/blob/28857b79898d6a6040b7d48f3781ebc2710f0279/src/cmap/connection_pool.ts

  1. connectionIsIdle is the only place that uses maxIdleTimeMS
  2. destroyConnectionIfPerished destroys idle connections based on connectionIsIdle
  3. destroyConnectionIfPerished only runs in ensureMinPoolSize() (which is a no-op if minPoolSize === 0) or processWaitQueue(), which runs when a new operation runs. The minPoolSize === 0 check seems a bit fishy, I'll ask MongoDB about that.

"I'm debugging an issue where my connection count increases over time like a memory leak." <-- does your connection count go above your maxPoolSize, which is 100 by default? The MongoDB Node driver does grow/shrink the number of connections between minPoolSize and maxPoolSize based on how many operations you run.

@vkarpov15
Copy link
Collaborator

Looks like there is already a ticket in the MongoDB driver JIRA for the minPoolSize === 0 check here: https://jira.mongodb.org/browse/NODE-6589.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants