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

Merged
merged 2 commits into from
Feb 12, 2025

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.

lib/connection.js Outdated Show resolved Hide resolved
@vkarpov15 vkarpov15 merged commit f1c6ad2 into master Feb 12, 2025
18 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15241-2 branch February 13, 2025 09:19
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