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

fix(model): avoid adding timeout on Model.init() buffering to avoid unintentional dangling open handles #15251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vkarpov15
Copy link
Collaborator

@vkarpov15 vkarpov15 commented Feb 11, 2025

Fix #15241

Summary

Make Model.init() skip buffering timeouts introduced in #15229

Based on discussion from #15241, I think this is a reasonable approach which gives us the benefits of #15229 without the implicit dangling handles. The following Jest test won't complain about open handles:

const mongoose = require('mongoose');

// Define a User schema and model
const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  age: Number
});

const User = mongoose.model('User', userSchema);

describe('Database connection', () => {
  it('test', function () {

  });
});

However, the following script will still error out with buffering timeout, as opposed to silently exiting with no error.

'use strict';

const mongoose = require('mongoose');

(async function() {
  await mongoose.connection.listCollections();
})();

So explicit connection helper calls will be subject to buffer timeouts, but we will ignore buffer timeouts for Model.init() because Model.init() is called implicitly and there are use cases where you may want to create a model without opening the underlying connection, like testing document functionality.

Examples

@vkarpov15 vkarpov15 added this to the 8.10.1 milestone Feb 11, 2025
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm this fixes the cases i had for typegoose without needing to set any options or other code changes.

Comment on lines +834 to +837
await new Promise(resolve => {
queueElement.fn = resolve;
this._queue.push(queueElement);
}),
new Promise(resolve => {
timeout = setTimeout(
() => {
timedOut = true;
resolve();
},
bufferTimeoutMS
);
})
]);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be de-duplicated by storing the promise in a variable before the if, and just directly awaiting it here and in the other branch adding it to the race.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Jest detect open handles in tests after upgrading from 8.9.6 to 8.10.0
2 participants