-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix: clear timeout in collection operations to prevent memory leak #15852
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
ba08ce0
ba491ca
728e039
f826b66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,14 +113,13 @@ NativeConnection.prototype.useDb = function(name, options) { | |
|
|
||
| newConn.name = name; | ||
|
|
||
| // push onto the otherDbs stack, this is used when state changes | ||
| this.otherDbs.push(newConn); | ||
| newConn.otherDbs.push(this); | ||
|
|
||
| // push onto the relatedDbs cache, this is used when state changes | ||
| if (options && options.useCache) { | ||
| this.relatedDbs[newConn.name] = newConn; | ||
| newConn.relatedDbs = this.relatedDbs; | ||
| // Add to relatedDbs for state synchronization and caching | ||
|
||
| this.relatedDbs[newConn.name] = newConn; | ||
| newConn.relatedDbs = this.relatedDbs; | ||
|
|
||
| // Ensure the parent connection is also tracked in the new connection's relatedDbs | ||
| if (this.name && !newConn.relatedDbs[this.name]) { | ||
| newConn.relatedDbs[this.name] = this; | ||
| } | ||
|
|
||
| return newConn; | ||
|
|
@@ -160,19 +159,21 @@ NativeConnection.prototype.aggregate = function aggregate(pipeline, options) { | |
| */ | ||
|
|
||
| NativeConnection.prototype.removeDb = function removeDb(name) { | ||
| const dbs = this.otherDbs.filter(db => db.name === name); | ||
| if (!dbs.length) { | ||
| const db = this.relatedDbs[name]; | ||
|
||
| if (!db) { | ||
| throw new MongooseError(`No connections to database "${name}" found`); | ||
| } | ||
|
|
||
| for (const db of dbs) { | ||
| db._closeCalled = true; | ||
| db._destroyCalled = true; | ||
| db._readyState = STATES.disconnected; | ||
| db.$wasForceClosed = true; | ||
| db._closeCalled = true; | ||
| db._destroyCalled = true; | ||
| db._readyState = STATES.disconnected; | ||
| db.$wasForceClosed = true; | ||
|
|
||
| // Remove from all related connections' relatedDbs | ||
| for (const relatedDb of Object.values(this.relatedDbs)) { | ||
|
||
| delete relatedDb.relatedDbs[name]; | ||
| } | ||
| delete this.relatedDbs[name]; | ||
| this.otherDbs = this.otherDbs.filter(db => db.name !== name); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -345,8 +346,10 @@ NativeConnection.prototype.createClient = async function createClient(uri, optio | |
|
|
||
| _setClient(this, client, options, dbName); | ||
|
|
||
| for (const db of this.otherDbs) { | ||
| _setClient(db, client, {}, db.name); | ||
| for (const db of Object.values(this.relatedDbs)) { | ||
|
||
| if (db !== this) { | ||
| _setClient(db, client, {}, db.name); | ||
| } | ||
| } | ||
| return this; | ||
| }; | ||
|
|
@@ -494,8 +497,10 @@ function _setClient(conn, client, options, dbName) { | |
|
|
||
| client.on('serverHeartbeatSucceeded', () => { | ||
| conn._lastHeartbeatAt = Date.now(); | ||
| for (const otherDb of conn.otherDbs) { | ||
| otherDb._lastHeartbeatAt = conn._lastHeartbeatAt; | ||
| for (const otherDb of Object.values(conn.relatedDbs)) { | ||
|
||
| if (otherDb !== conn) { | ||
| otherDb._lastHeartbeatAt = conn._lastHeartbeatAt; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const mongoose = require('../index'); | ||
|
|
||
| describe('Collection timeout cleanup', function() { | ||
|
||
| let db; | ||
|
|
||
| before(async function() { | ||
| db = await mongoose.createConnection('mongodb://127.0.0.1:27017/mongoose_test').asPromise(); | ||
| }); | ||
|
|
||
| after(async function() { | ||
| await db.close(); | ||
| }); | ||
|
|
||
| it('should clear timeout on successful operations', async function() { | ||
| const TestModel = db.model('Test', { name: String }); | ||
|
|
||
| // Track active handles before operations | ||
| const initialHandles = process._getActiveHandles().length; | ||
|
|
||
| // Execute multiple operations that should complete successfully | ||
| for (let i = 0; i < 10; i++) { | ||
| await TestModel.find({}).exec(); | ||
| } | ||
|
|
||
| // Allow some time for any lingering timeouts | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| // Active handles should not have grown significantly | ||
| const finalHandles = process._getActiveHandles().length; | ||
| assert.ok(finalHandles <= initialHandles + 2, | ||
| `Expected handles to remain stable, but grew from ${initialHandles} to ${finalHandles}`); | ||
| }); | ||
|
|
||
| it('should clear timeout on operation errors', async function() { | ||
| const TestModel = db.model('Test2', { name: String }); | ||
|
|
||
| const initialHandles = process._getActiveHandles().length; | ||
|
|
||
| // Execute operations that will fail | ||
| for (let i = 0; i < 5; i++) { | ||
| try { | ||
| await TestModel.collection.findOne({ $invalidOperator: true }); | ||
| } catch (err) { | ||
| // Expected to fail | ||
| } | ||
| } | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| const finalHandles = process._getActiveHandles().length; | ||
| assert.ok(finalHandles <= initialHandles + 2, | ||
| `Expected handles to remain stable after errors, but grew from ${initialHandles} to ${finalHandles}`); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please undo this change
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkarpov15 I've reverted all the unrelated otherDbs changes as requested. The PR now contains only the setTimeout memory leak fix with clearTimeout() calls added to the success and error paths.
Is this ready to merge, or are there any other changes needed?