Skip to content

Memory leak caused by calling findOne() on a Time Series collection #13829

@noMoneyNoProblems

Description

@noMoneyNoProblems

Prerequisites

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

Mongoose version

6.3.0, 7.5.0

Node.js version

18.16.0

MongoDB server version

5.0

Typescript version (if applicable)

No response

Description

Calling findOne() on a time series collections seems to create a ClientSession that persists in memory after the query executes.

As a contrast, this problem does not occur when calling find().limit(1) on a time series collection. Nor does this problem occur when calling findOne() on a non-time series collection.

Steps to Reproduce

  • Run the below script with node
  • Observe the memory usage continuously increasing
  • Take heap snapshots of the process at different points, and observe that ClientSessions do not get deleted

Reproduction Script

`

'use strict';

const mongoose = require('mongoose');
const v8 = require('v8');

void async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

const testSchema = new mongoose.Schema(
    {
        data: String,
        createdAt: { type: Date, default: Date.now },
    },
    {
        timeseries: {
            timeField: 'createdAt'
        },
        expireAfterSeconds: 60 * 60 * 24 * 7
    }
);

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

await TestModel.deleteMany({});
const docs = [];
for (let i = 0; i < 1000; ++i) {
    docs.push({ data: '0'.repeat(1024 * 2 * 10) });
}
await TestModel.create(docs);
console.log('Created docs');

let start = Date.now();
let count = 0;

while (++count) {
    const res = await TestModel.findOne({}).lean().exec();
    if (count % 1000 === 0) {
        console.log(`{ x: ${Date.now() - start}, y: ${v8.getHeapStatistics().used_heap_size} },`);
    }
    await new Promise(resolve => setTimeout(resolve, 10));
}}();

`

Output on Mongoose 7.5.0:

{ x: 12402, y: 42433560 },
{ x: 24895, y: 30014624 },
{ x: 37086, y: 29719928 },
{ x: 49291, y: 35708824 },
{ x: 61408, y: 41950792 },
{ x: 73720, y: 47739128 },
{ x: 87937, y: 42759744 },
{ x: 102594, y: 44080096 },
{ x: 117495, y: 47526592 },
{ x: 129495, y: 54821872 },
{ x: 141727, y: 62949944 },
{ x: 154910, y: 61238800 },
{ x: 168411, y: 67568032 },
{ x: 181743, y: 70083768 },
{ x: 200170, y: 63270080 },
{ x: 213501, y: 68497400 },
{ x: 226923, y: 74216264 },
{ x: 240495, y: 75517744 },
{ x: 253685, y: 77082480 },
{ x: 266875, y: 78231992 },
{ x: 280405, y: 79718080 },
{ x: 301401, y: 85207552 },

The count of ClientSessions in the heap snapshot reached 21466 when I stopped the process.

Expected Behavior

  1. Calling findOne() on a time series collection creates a ClientSession that doesn't persist after the query finishes execution.
  2. Calling findOne() repeatedly on a time series collection does not cause the memory usage of the node process to continuously increase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    underlying library issueThis issue is a bug with an underlying library, like the MongoDB driver or mongodb-core

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions