mongoose: Connection timeout 10000ms when upgraded mongoose from 7.4.5 to 8.7.1
Prerequisites
- I have written a descriptive issue title
Mongoose version
8.7.1
Node.js version
20.x
MongoDB version
6.9
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
Last night, I upgraded my mongodb and mongoose from versions 5.8.1 to 6.9 and. 7.4.5 to 8.7.1 respectively.
I tested on my local. Everything worked fine. I promoted the changes to test env - AWS amplify - things were fine. Then, I moved the changes to production.
My db is hosted on mongo atlas serverless cluster.
On production, sometimes same queries will work fine but other times give timeout error with 10000ms. I mean same queries behaving differently.
I have been scratching my head because both test and prod env are exactly similar except that prod is connected to my domain name. Please assist if someone has knowledge about this.
sharing my connection code and one query that fails/passes abruptly.
import { ServerApiVersion } from "mongodb";
import mongoose from "mongoose";
const uri = process.env.MONGODB_URI as string;
const connection: {
isConnected?: boolean;
} = {};
const dbConnect = async () => {
if (connection.isConnected) return;
mongoose.set("strictQuery", false);
const db = await mongoose.connect(uri);
connection.isConnected = Boolean(db.connections[0].readyState);
};
export default dbConnect;
export async function GET(req: Request) {
try {
await apiHandler(req, "");
const countries: any = await Countries.find({}).exec();
return NextResponse.json(countries, { status: 200 });
} catch (error: any) {
console.log("wb saas: countries");
console.log(
typeof error !== "string"
? error.message
? error.message
: (error || {}).toString()
: error
);
return errorHandler(error);
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 30
Will there be any fix soon? With this now being public but not being able to update mongoose 8.6.4 to any newer version it creates some urgency from my perspective.
I have the same issue. Downgrading to mongoose 8.6.x solved the issue. I tried everything, even created a new mongodb atlas cluster to make sure it’s not the cluster, even though connecting to the db worked fine from other tools. There’s definitely something wrong with mongoose or the mongodb package.
Worth mentioning: It only fails to connect with the mongodb+srv:// scheme.
@sukhminderx
I did, no success.
Hello,
I add my experience on this for other people:
Before the npm audit, no error, after, the mentionned error started to pop totally randomly everywhere during runtime, not just after startup. Setting bufferCommands: false immediately solved the issue. Definitely a mongoose glitch between v8.0.4 and v8.9.5.
We found the issue was introduced in 8.7. We were on 8.6, but only saw the issues when we upgraded to 8.7.
There is unfortunately no known way to reproduce this issue locally.
Can you confirm whether this buffering timed out error occurs right after server start, or after the server has been running for some time? If after the server has been running for some time, you can try setting
bufferCommands: falseon your connection, like the followingThat will disable buffering, which may make the issue go away if it is a Mongoose issue, or it may cause a MongooseServerSelectionError to happen instead of the buffering timed out error.
the same issue. Mongo: 8.0 Node: 20.13.1 Mongoose 8.7.1
We’re also experiencing a very similar issue.
Using MongoDB Server 8.0 in Atlas and locally in Docker. Node: 20.11.1 Mongoose 8.7.1
Everything works intermittently, but lots of buffering error across lots of different find’s and aggregates in all of our microservices.
Posted about it here: https://www.mongodb.com/community/forums/t/buffering-timed-out-after-10000ms-8-7-0-mongoose-node-20-10-0/299932