question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timeout when debuggin Node.js application locally or remotely

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

2reactions
sw360cabcommented, Dec 14, 2020

@jaffarc it looks more that you have EAI_AGAIN variable undefined rather than the timeout I am exposing

0reactions
sw360cabcommented, May 17, 2021

Apparently 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debug Node.js Apps using Visual Studio Code
There are a few ways you can debug your Node.js programs in VS Code: ... guide on how to debug a remote Node.js...
Read more >
Navigation timeout is disabled when remote debugging is ...
I used the following puppeteer configuration with NodeJS 10 framework and it worked for me const PUPPETEER_OPTIONS = { headless: true, ...
Read more >
A Complete Guide to Timeouts in Node.js - Better Stack
Assigning timeout values prevents network operations in Node.js from blocking indefinitely. This article provides extensive instruction on ...
Read more >
Nodejs debugging - vscode-docs1
The Node.js debugger supports remote debugging for versions of Node.js >= 4.x. Specify a remote host via the address attribute. By ...
Read more >
Debugging - Getting Started - Node.js
See the section on 'Enabling remote debugging scenarios' on some advice on how to safely allow remote debugger clients to connect. Local applications...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found