-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Model init can hang #15201
Comments
Worth noting that this problem exists in mongoose 8.8.4 as well as 8.9.5 - however, it appears to have got more prevelant in 8.9.x. I think this is because the heartbeat emit happens later in 8.9.x after calling "openUri" (or any similar connection API). I am finding that with a heartbeat of 2.5s, it is not uncommon for there to be no heartbeat for over 5s...which exaserbates the issue here:
which only considers there to be a disconnection when 2x the heartbeat frequency has elapsed:
|
Another thing worth trying that shows the issue without needing to breakpoint the code.
Things that may help?:
These ideas do not fix the underlying issue, i.e. _waitForConnect hangs. But I think the out of date _lastHeartbeatAt has certainly exasperated the issue and made it much more likely that our unit tests hang. |
I was able to repro using the following script const mongoose = require('mongoose');
async function testMongoose() {
const db = await mongoose.connect('mongodb://127.0.0.1/testMongoose', {heartbeatFrequencyMS: 2500, bufferTimeoutMS: 1000});
let firstTime = true;
// Wait for valid heartbeat to be registered.
db.connection.db.client.on('serverHeartbeatSucceeded', async () => {
console.log(`heartbeat - readyState ${db.connection.readyState}`);
await db.connection.close();
console.log('Wait');
await new Promise(resolve => setTimeout(resolve, 5100));
const blogSchema = new mongoose.Schema({author: 'ObjectId', title: String, body: String, date: Date}, { });
const blogModel = mongoose.model('blog', blogSchema);
const promise = blogModel.init();
await db.connection.openUri('mongodb://127.0.0.1/testMongoose', {heartbeatFrequencyMS: 2500});
console.log('Wait for init');
await promise;
console.log('Done');
});
}
testMongoose().then(() => {}); You're correct that a stale |
feat(connection): make connection helpers respect bufferTimeoutMS
Hi - unfortunately this has not fixed the issue. I copied your script above and had two different issues:
I verified that I had correctly installed 8.9.7 by checking the "mongoose" version at runtime (just in case my script was somehow grabbing a different version). I'm running Node 20.14.0. I feel like maybe I'm doing something wrong??? But can't think what. Having said this, I have also tested 8.9.7 in our full unit test suite and it's still hanging in the same place as before. |
Sorry - I think I've misread the notes - I thought the fix had been merged into both 8.9.7 (released) and 8.10.x (pending release). |
We merged the PR and fix into 8.10, and we just shipped 8.10.0. We did not put this change into 8.9.7. |
I can confirm that 8.10.0 has fixed the issue. Thank you for the quick turnaround on this issue - much appreciated. |
Prerequisites
Mongoose version
8.9.5
Node.js version
20.14.0
MongoDB server version
8.0.1
Typescript version (if applicable)
No response
Description
It is possible for model.init to fail to complete. This happens if the heartbeat expires when making the call; the code hangs inside "Connection.prototype.createCollection" - internally this function never returns from the call to "_waitForConnect".
Steps to Reproduce
Run this self contained script - breakpoint the "init" line for long enough to expire the heartbeat.
I found it useful to update this mongoose function to show that the readyState becomes 0 and that causes the "end _waitForConnect" to never happen.
Expected Behavior
model.init should never hang. From the test code above you can see that the heartbeat signal correctly continues to be emitted, and even the connection readyState gets updated, but _waitForConnect never returns.
console output when fails:
console output when succeeds:
The text was updated successfully, but these errors were encountered: