Timeout when debuggin Node.js application locally or remotely
See original GitHub issueWhen debugging a Node.js application both locally or remotely there is an annoying connection timeout that does not occur at all when the application is running without debug mode (not meaning strictly Mongoose debug) or stopping ar breakpoints.
The application is connecting to MongoDB via Mongoose with the following configuration:
const connectionUri = `mongodb://${dbHost}/${dbName}`;
mongoose.connect(connectionUri, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
/*
* current Server Discovery and Monitoring engine is deprecated,
* and will be removed in a future version.
* To use the new Server Discover and Monitoring engine,
* pass option { useUnifiedTopology: true } to the MongoClient constructor.
* Set to true to opt in to using the MongoDB driver's new connection management engine.
* You should set this option to true, except for the unlikely case
* that it prevents you from maintaining a stable connection.
*/
useUnifiedTopology: true,
// cfr. https://mongoosejs.com/docs/connections.html#error-handling
serverSelectionTimeoutMS: 5000
});
Depending on the environment the connection to MongoDB can be toward:
- localhost
- remote host
- via Docker network
- MongoDB is usually running in a container exposing port 27017 (whether not in the same network stack with application)
- At the moment nor replica set or other particular topolgies are in charge in this environment configuration
Lib versions
- NodeJS: 14.3
- Mongoose: 5.10.11
- Mongo: 4.4
Apparently this did not happen with previous versions of Mongoose. Haven’t tried yet, but the configuration above remained the same.
Step to reproduce
Run a Node.js app connecting to MongoDB via Mongoose in debug, stop at a breakpoint. Waiting few seconds and the following error message should show up:
app | 2020-12-10T10:19:27.053Z | ERROR | MongooseServerSelectionError: connection <monitor> to 172.19.0.2:27017 timed out
app | at Function.Model.$wrapCallback (/usr/src/app/node_modules/mongoose/lib/model.js:4856:32)
app | at /usr/src/app/node_modules/mongoose/lib/query.js:4393:21
app | at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
app | at model.Query.exec (/usr/src/app/node_modules/mongoose/lib/query.js:4392:10)
app | at _execPopulateQuery (/usr/src/app/node_modules/mongoose/lib/model.js:4502:9)
app | at populate (/usr/src/app/node_modules/mongoose/lib/model.js:4423:24)
app | at _populate (/usr/src/app/node_modules/mongoose/lib/model.js:4291:5)
app | at /usr/src/app/node_modules/mongoose/lib/model.js:4267:5
app | at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
app | at Function.Model.populate (/usr/src/app/node_modules/mongoose/lib/model.js:4265:10)
app | at model.populate (/usr/src/app/node_modules/mongoose/lib/document.js:3805:19)
app | at /usr/src/app/node_modules/mongoose/lib/document.js:3856:10
app | at /usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
app | at new Promise (<anonymous>)
app | at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
app | at model.Document.execPopulate (/usr/src/app/node_modules/mongoose/lib/document.js:3855:10)
app | at /usr/src/app/models/Event.js:198:8
app | at new Promise (<anonymous>)
app | at model.EventSchema.methods.decorate (/usr/src/app/models/Event.js:196:10)
app | at /usr/src/app/routes/api/eventApi.js:773:46
app | at Array.map (<anonymous>)
app | at /usr/src/app/routes/api/eventApi.js:773:33
app | at processTicksAndRejections (internal/process/task_queues.js:93:5) {
app | reason: **TopologyDescription** {
app | type: 'Single',
app | setName: null,
app | maxSetVersion: null,
app | maxElectionId: null,
app | servers: Map(1) { 'mongo_klosed:27017' => [ServerDescription] },
app | stale: false,
app | compatible: true,
app | compatibilityError: null,
app | logicalSessionTimeoutMinutes: null,
app | heartbeatFrequencyMS: 10000,
app | localThresholdMS: 15,
app | commonWireVersion: 9
app | },
app | methodName: 'geoQuery',
app | httpStatusCode: undefined
app | }
Step to resolve
Apparently forcing
useUnifiedTopology: false
make the issue disappear. But as the comment left above says:
You should set this option to true, except for the unlikely case that it prevents you from maintaining a stable connection.
On the other side, it is now quite impossible to debug the application remotely when in Staging environment (or even worse production).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7
@jaffarc it looks more that you have
EAI_AGAIN
variableundefined
rather than the timeout I am exposingApparently with newest versions of mongoose lib this rarely happens and it is very hard to reproduce. I will update the issue if encounter it again.