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

{ useUnifiedTopology: true } leads to MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms #8180

Closed
tschaffter opened this issue Sep 19, 2019 · 78 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@tschaffter
Copy link

Do you want to request a feature or report a bug?

A bug.

What is the current behavior?

After updating to Mongoose 5.7.1, the following warning appeared:

(node:41563) DeprecationWarning: 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.
    at parseFn (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:312:5)
    at parseConnectionString (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/core/uri_parser.js:628:3)
    at connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:266:3)
    at ConnectOperation.execute (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/connect.js:191:5)
    at executeOperation (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/operations/execute_operation.js:83:26)
    at MongoClient.connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:216:10)
    at Promise (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/connection.js:632:12)
    at Promise._execute (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/debuggability.js:313:9)
    at Promise._resolveFromExecutor (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/promise.js:488:18)
    at new Promise (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/bluebird/js/release/promise.js:79:10)
    at NativeConnection.Connection.openUri (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/connection.js:629:19)
    at Mongoose.connect (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/mongoose/lib/index.js:327:15)
    at Object.connect (/Users/tschaffter/dev/PHCCollaborationPortal/server/app.js:17:44)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module._compile (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/tschaffter/dev/PHCCollaborationPortal/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/tschaffter/dev/PHCCollaborationPortal/server/index.js:12:28)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

I added useUnifiedTopology: true to my mongoose config object as suggested by the message, even though I'm not using MongoDB replica set or sharded cluster. Here is how I configure and connect using mongoose:

        options: {
            // https://mongoosejs.com/docs/deprecations.html
            useNewUrlParser: true,
            useFindAndModify: false,
            useCreateIndex: true,
            useUnifiedTopology: true,
            reconnectTries: 30,
            reconnectInterval: 500, // in ms
        }
    },

and here is where I used this mongo.options object:

// Connect to MongoDB
const mongooseConnectionPromise = mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', err => {
    console.error(`MongoDB connection error: ${err}`);
    process.exit(-1); // eslint-disable-line no-process-exit
});

The warning is gone but the issue is that mongoose is now no longer able to connect:

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms
[nodemon] app crashed - waiting for file changes before starting...

Here is how I run MongoDB using docker during development:

docker run -p ${MONGO_PORT}:${MONGO_PORT} --name mongo -d mongo

If the current behavior is a bug, please provide the steps to reproduce.

See above

What is the expected behavior?

Mongoose should remove the deprecation warning and run fine when using useUnifiedTopology: true as mongoose suggests.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node: v10.16.2
Mongoose: 5.7.1 (latest)
MongoDB: db version v4.2.0

Related issues:

@vkarpov15
Copy link
Collaborator

What docker image do you use? Also, do you have a stack trace for the 'Server selection timed out' message?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Sep 23, 2019
@Novaras
Copy link

Novaras commented Sep 26, 2019

Hey, just poking in here to report that I had the same issue.

I myself am not using Docker or any vm. Also to note is that this doesn't seem to happen all the time, however today I was unable to retreive any of my data due to timeouts (30k), and the issue was immediately resolved when removing the useUnifiedTopology flag.

I'm connecting to an Atlas cluster.

Mongoose is 5.7.1
MongoDb is 3.3.2
Node is 12.9.1

Connecting like this:

mongoose.createConnection(uri,
	{
		useCreateIndex: true,
		useNewUrlParser: true,
		useUnifiedTopology: true, // commented out currently
		dbName: db_name
	});

The model is created like this:

import mongoose from 'mongoose';
import * as db_conns from '../db-connections'; // this is an object of connections generated via createConnection

// ... schema definition, for a model called 'article' from a collection called 'article'

// `site_content` is the name of a connection
export default db_conns.connections.site_content.model(`article`, schema, `article`);

Then it's fetched like this:

// ...
await query.model.find(query_expr).limit(30); // query_expr can change, however even an empty expression ({}) will cause the same issue
// ...

The find call hangs and issues the timeout error.

@tschaffter
Copy link
Author

@vkarpov15 I'm using the latest mongo image that provide MongoDB v4.2.0.
https://hub.docker.com/_/mongo

@vkarpov15 vkarpov15 removed the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Sep 30, 2019
@vkarpov15 vkarpov15 added this to the 5.7.4 milestone Sep 30, 2019
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Sep 30, 2019
@indrajit-rathod
Copy link

I am also getting the same error after adding the { useUnifiedTopology: true } , although the error is randomly occuring. There is something definitely wrong. I am using the latest mongo image.

@pabloabreu1986
Copy link

mongoose
.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(Error, err.message);
});
When it starts I get at the console:
[Function: Error] { stackTraceLimit: 16, prepareStackTrace: undefined } connection 0 to acccluster-shard-00-01-xx47j.mongodb.net:27017 closed

@shanmugarajbe
Copy link

Any solutions? Even I am facing this issue now...

@samimhm
Copy link

samimhm commented Oct 3, 2019

I have the same issue. I'm a junior programmer and it's my first time using nodejs-express-graphql-mongoose-mongodbAtlas.

(node:9392) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout.setTimeout (MyAppPath\graphql2\node_modules\mongodb\lib\core\sdam\topology.js:850:16)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
(node:9392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
events.js:174
throw er; // Unhandled 'error' event
^

Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at TLSSocket. (MyAppPath\node_modules\mongodb\lib\core\connection\connection.js:321:10)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)

@clarkmcc
Copy link

clarkmcc commented Oct 3, 2019

We're also getting the same error on our production server after enabling useUnifiedTopology.

MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.setTimeout [as _onTimeout] (/opt/app/node_modules/mongodb/lib/core/sdam/topology.js:850:16)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)

@indrajit-rathod
Copy link

I started to encounter another problem now
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 topologyDescriptionChanged listeners added.

When I searched for this warning I came across this mongoDB document
https://jira.mongodb.org/browse/NODE-2123

It states both the errors
"Server selection timed out " as well as "MaxListenersExceededWarning" and both the errors are due to the unified topology change. I guess this was a breaking change which was never mentioned.

Since I am encountering this error on my live server I have no choice but to set { useUnifiedTopology: false }. I hope someone from the community can focus on this issue and resolve it or provide some suggestions to resolve it.

@SimeonStoykov
Copy link

I am also getting both errors (Server selection timed out after 30000 ms and MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11) when using useUnifiedTopology so this is happening to everyone that uses this option I guess.

@vkarpov15
Copy link
Collaborator

I've tried to repro this, but I haven't been able to repro. I tried a few options:

  1. Killing the server right before querying

  2. Querying with a disconnected connection

  3. Successfully executing a query

I have yet to see this error message in any case. Can you please modify the below script to demonstrate this issue?

'use strict';

const mongoose = require('mongoose');

run().catch(err => console.log(err));

async function run() {
  mongoose.set('useUnifiedTopology', true);
  mongoose.set('useNewUrlParser', true);

  await mongoose.connect('mongodb://localhost:27017/test');

  const Model = mongoose.model('Test', new mongoose.Schema({ name: String }));
  
  console.log('Query...');
  await Model.findOne();
  console.log('Done');
}

Or at least provide some information about what version of Mongoose you're using; whether you're using a standalone server, replica set, or sharded cluster; and whether you're using Mongoose directly or via another npm module?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 7, 2019
@vkarpov15 vkarpov15 removed this from the 5.7.4 milestone Oct 7, 2019
@Novaras
Copy link

Novaras commented Oct 8, 2019

I'm fairly convinced this is not an issue with mongoose, but with Mongo itself.

@vkarpov15, please see my comment, I outlined my environment there. I'm using mongoose directly.

@indrajit-rathod
Copy link

@vkarpov15 for me this issue appears randomly every 2-3 days when the server is running so even if I provide you the code you will have to monitor the server continuously.

@indrajit-rathod
Copy link

Has anyone got around this issue?

@Novaras
Copy link

Novaras commented Oct 10, 2019

Has anyone got around this issue?

You can just not use this flag for the time being. Mongo will warn you that it will be disabled in future or something but for the time being it seems to totally solve this issue.

@basma-abdin
Copy link

basma-abdin commented Oct 10, 2019

Has anyone got around this issue?

You can just not use this flag for the time being. Mongo will warn you that it will be disabled in future or something but for the time being it seems to totally solve this issue.

when i don't use the flag useUnifiedTopology: true , i have this error and the app stopped,
mongoose veriosn: 5.7.4
Screenshot from 2019-10-10 11-07-11

@rpedroni
Copy link

Can confirm that commenting useUnifiedTopology out solves the problem in my build. Also happening in a (apparently) random fashion.

@bke-daniel
Copy link

@rpedroni Sadly, it doesn't for us...

@leovazquezz1
Copy link

leovazquezz1 commented Oct 16, 2019

Also happening here. Our production server responds with the bellow message. I gues I will comment useUnifiedTopology for now

MongoDB Connection Options

var options = {
    useCreateIndex: true,
    useNewUrlParser: true,
    useFindAndModify: false,
    useUnifiedTopology: true,
    promiseLibrary: global.Promise
};

Server Log

MongoTimeoutError: Server selection timed out after 30000 ms 
Oct 16 12:56:31 app/web.1:     at Timeout.setTimeout (/app/node_modules/mongodb/lib/core/sdam/topology.js:850:16) 
Oct 16 12:56:31 app/web.1:     at Shim.applySegment (/app/node_modules/newrelic/lib/shim/shim.js:1424:20) 
Oct 16 12:56:31 app/web.1:     at Timeout.wrappedCallback [as _onTimeout] (/app/node_modules/newrelic/lib/shim/shim.js:1281:21) 
Oct 16 12:56:31 app/web.1:     at ontimeout (timers.js:436:11) 
Oct 16 12:56:31 app/web.1:     at tryOnTimeout (timers.js:300:5) 
Oct 16 12:56:31 app/web.1:     at listOnTimeout (timers.js:263:5) 
Oct 16 12:56:31 app/web.1:     at Timer.processTimers (timers.js:223:10) 

@rvanmil
Copy link

rvanmil commented Oct 16, 2019

We are having this issue as well and we do not use mongoose. It appears to be a problem with the MongoDB driver for Node.js itself. The issue has also been reported over here https://jira.mongodb.org/browse/NODE-2249
Version 3.3.3 of the driver which was published today should add more details to the MongoTimeoutError. It is also recommended to update the driver due to a bugfix.

This error is the result of the driver being unable to connect to a server satisfying the read preference of a given operation (or initial connect). This could be for a number of reasons, including your connection string being invalid or nodes in your cluster being down. If you update to the latest v3.3.3 of the driver we include a new reason field with the MongoTimeoutError which will help us further investigate your issue. Additionally, this version includes a bug fix which would result in a timeout error on initial connect if any member of a replicaset was not available. Please update your driver, and let us know if you continue to experience this issue.

Quoted from https://jira.mongodb.org/browse/NODE-2249?focusedCommentId=2485028&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2485028

@vkarpov15 vkarpov15 removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Oct 17, 2019
@mbroadst
Copy link

mbroadst commented Nov 6, 2019

@zhenwan this looks like an issue downloading the abbrev package. My guess is that there are issues with your package-lock.json, please try deleting that file and running npm install again.

@rvanmil
Copy link

rvanmil commented Nov 6, 2019

I was able to reproduce the error and check if it was solved with the v3.3.4-rc0 driver using the following setup:

  • Node.js webserver exposing a simple http api GET /api/todos which queries a todo MongoDB collection which contains some sample data
  • Script which calls the api every 500ms running locally on my machine
  • Once the script is running, trigger a failover (restart the primary) on MongoDB (on Atlas I was able to do this on an M10 cluster by selecting the "Test Failover" menu option)

The results of this test were as follows:

✅ Mongodb Node.js 3.3.2 useUnifiedTopology false

> MongoDB failover triggered
> MongoDB 'reconnect' event
> A few http calls with +/- 5000ms response times
> MongoDB failover finished
> Everything back to normal

❌ Mongodb Node.js 3.3.2 useUnifiedTopology true

> MongoDB failover triggered
> MongoDB 'close' event
> Many MongoNetworkError errors
    {
      "name": "MongoNetworkError",
      "stack": "Error: connect ECONNREFUSED <ip_redacted>:27017
          at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14)",
      "errorLabels": [
        "TransientTransactionError"
      ]
    }
> EventEmitter memory leak warning
    (node:18) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 topologyDescriptionChanged listeners added to [NativeTopology]. Use emitter.setMaxListeners() to increase limit
> Many MongoTimeoutError errors
    {
      "name": "MongoTimeoutError",
      "stack": "MongoTimeoutError: Server selection timed out after 30000 ms
          at Timeout._onTimeout (/app/node_modules/mongodb/lib/core/sdam/topology.js:850:16)
          at listOnTimeout (internal/timers.js:531:17)
          at processTimers (internal/timers.js:475:7)"
    }
> MongoDB failover finished
> Server unable to recover MongoDB connection
> Server restarted manually
> Everything back to normal

⚠️ Mongodb Node.js 3.3.4-rc0 useUnifiedTopology true

> MongoDB failover triggered
> MongoDB 'reconnect' event expected but not seen
> A few http calls with +/- 5000ms response times
> A couple failed http calls:
    {
      "name": "MongoError",
      "stack": "MongoError: Cache Reader No keys found for HMAC that is valid for time: { ts: Timestamp(1573064656, 1) } with id: 6756219367492419585
          at Connection.<anonymous> (/app/node_modules/mongodb/lib/core/connection/pool.js:451:61)
          at Connection.emit (events.js:210:5)
          at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:368:10)
          at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:537:15)
          at TLSSocket.emit (events.js:210:5)
          at addChunk (_stream_readable.js:308:12)
          at readableAddChunk (_stream_readable.js:289:11)
          at TLSSocket.Readable.push (_stream_readable.js:223:10)
          at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23)",
      "ok": 0,
      "errmsg": "Cache Reader No keys found for HMAC that is valid for time: { ts: Timestamp(1573064656, 1) } with id: 6756219367492419585",
      "code": 211,
      "codeName": "KeyNotFound"
    }
> MongoDB failover finished
> Everything back to normal

So it seems to work with v3.3.4-rc0, the server is able to recover the connection and resume serving data from MongoDB. There are still two issues though:

  • The 'reconnect' event seems to be missing
  • A couple requests fail with a "Cache Reader No keys found for HMAC that is valid for time" error message

@zhenwan
Copy link

zhenwan commented Nov 6, 2019

@mbroadst yes, deleting package-lock.json solved my missing file issue.

But I still get the error:
2019-11-06T12:52:10.35-0600 [CELL/0] OUT Container became healthy
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR /home/vcap/app/node_modules/connect-mongo/src/index.js:135
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR throw err
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR ^
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR Error: Server selection timed out after 30000 ms
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR at Timeout.setTimeout [as _onTimeout] (/home/vcap/app/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:878:9)
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR at ontimeout (timers.js:469:11)
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR at tryOnTimeout (timers.js:304:5)
2019-11-06T12:52:39.06-0600 [APP/PROC/WEB/0] ERR at Timer.listOnTimeout (timers.js:264:5)
2019-11-06T12:52:39.35-0600 [APP/PROC/WEB/0] OUT Exit status 1

@mbroadst
Copy link

mbroadst commented Nov 6, 2019

@zhenwan it looks like it might be related to not being able to connect to your cluster on initial connect. Also your backtrace to topology.js:878 curiously does not point to a place where an error is generated, nor are you receiving the correct type MongoTimeoutError.

Can you please open a new jira ticket so we can triage your particular case. It looks like we'll need more detail than what you've pasted here. Thank you!

@mmmmoj
Copy link

mmmmoj commented Nov 7, 2019

@mbroadst
Thanks for helping fix the issue. I just want to say, using v3.3.4-rc0 is not solving my problem. I'm still getting this message

MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.setTimeout (api/node_modules/mongodb/lib/core/sdam/topology.js:899:9)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)

In my case, I'm using loopback-connector-mongodb, but I setup my package.json to force using last version of mongodb in resolutions as follow

"resolutions": {
    "mongodb": "^3.3.4-rc0"
  }

my connection parameters as follow

"host": "127.0.0.1",
 "port": 27017,
"database": "***",
"password": "***",
"user": "***",
"authSource": "***",
"useNewUrlParser": true,
"enableGeoIndexing": true

below is my environment parameters

MongoDB server version: 3.6.3 on localhost
yarn v1.19.1
node v8.16.2 

Thanks in advance

update
I also tried to add useUnifiedTopology: true to connection parameters, but got same result.

@bigsee
Copy link

bigsee commented Nov 7, 2019

Another confirmation here, from someone using mongoose directly, that useUnifiedTopology: false works for me.

@mbroadst thank you for the update from MongoDB's end. Excuse my ignorance, but will the aforementioned fix in mongodb v3.3.4-rc0 cascade down to mongoose at some point? Slightly uncomfortable with the idea of embracing deprecated features as a workaround in a prod environment.

@mbroadst
Copy link

mbroadst commented Nov 7, 2019

@mmmmoj similar to @zhenwan's question above, your stack trace seems to indicate you are not in fact running v3.3.4-rc0. Maybe try removing your package-lock.json? Can you please file a jira ticket so we can delve into the specifics of your case.

@mbroadst
Copy link

mbroadst commented Nov 7, 2019

@bigsee (man GitHub does not like autocompleting your name!) Glad to hear it works for you, and you're welcome for the update - our community is very important to us. I'll let @vkarpov15 speak for the timing of the release, but we work closely and I'm sure he's interested in fixing this for y'all :)

@mmmmoj
Copy link

mmmmoj commented Nov 7, 2019

@mbroadst
done! Thanks
NODE-2313

@bigsee
Copy link

bigsee commented Nov 7, 2019

@mbroadst ha! sorry for the awkward name!

it turns out I might have spoken too soon, perhaps given the random nature of the error appearing. On a cold start of my app, along with the expected deprecation warning, I now see this slightly scarier error in the logs:

Unhandled rejection: { MongoNetworkError: failed to connect to server [myserver.mlab.com:55841] on first connect [{ MongoNetworkError: connection 0 to myserver.mlab.com:55841 timed out
    at Socket.<anonymous> (/var/task/node_modules/mongodb/lib/core/connection/connection.js:335:7)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:420:8)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }]
    at Pool.<anonymous> (/var/task/node_modules/mongodb/lib/core/topologies/server.js:431:11)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at connect (/var/task/node_modules/mongodb/lib/core/connection/pool.js:580:14)
    at callback (/var/task/node_modules/mongodb/lib/core/connection/connect.js:109:5)
    at provider.auth.err (/var/task/node_modules/mongodb/lib/core/connection/connect.js:352:21)
    at _authenticateSingleConnection (/var/task/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11)
    at sendAuthCommand (/var/task/node_modules/mongodb/lib/core/auth/scram.js:177:16)
    at Connection.errorHandler (/var/task/node_modules/mongodb/lib/core/connection/connect.js:321:5)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/var/task/node_modules/mongodb/lib/core/connection/connection.js:333:10)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

It used to be MongoTimeoutError but now it's MongoNetworkError... ...any ideas?

@mbroadst
Copy link

mbroadst commented Nov 7, 2019

@bigsee that error would indicate that you are not using the unified topology. Please enable it by passing useUnifiedTopology: true to your MongoClient

@bigsee
Copy link

bigsee commented Nov 7, 2019

@mbroadst that's correct, which I believe is the point of this issue. Apologies if I'm wrong on that.

When I pass useUnifiedTopology: true the deprecation warning disappears but my app randomly crashes with the title error of this issue MongoTimeoutError: Server selection timed out after 30000 ms.

As with @vkarpov15 and others above, I have not been able to reproduce this issue consistently. The error happens a few times a day resulting in 502 and horrible UX from time to time errors. It seems to be a new issue.

As reported above, the workaround of setting useUnifiedTopology: false (or commenting out) prevents the error in this issue, but restores the deprecation warning and now the slightly scarier MongoNetworkError above. As an aside, this doesn't actually kill my app but does worry me that it will for a user at some point.

@mbroadst
Copy link

mbroadst commented Nov 7, 2019

@bigsee the MongoNetworkError you are experiencing is not new, and it means that the driver was unable to make an initial connection to a cluster (incidentally if some sort of race condition between your app starting and your cluster starting is causing this, then the unified topology should help here as well).

The point of this issue is that people were following the guidance to use the unified topology, and when they did they were experiencing spurious timeout errors. v3.3.4-rc0 was released to address that specific issue, so the current guidance is to use that version which should remove the deprecation notice and correct the spurious timeout issues people have encountered.

I should also say: this version will not prevent all timeout errors. That error is being caused by the driver's inability to do an initial connect to a cluster, which would indicate some sort of configuration (connection string) or network error on your part. With the unified topology this will show up as a MongoTimeoutError with a reason field indicating the network error that caused the timeout on initial connect.

@bigsee
Copy link

bigsee commented Nov 7, 2019

@mbroadst I didn't mean to suggest that the MongoNetworkError was new in my last comment, so apologies if that is how it read. It's the `MongoTimeoutError that is new. To hopefully be clearer, this has been my process:

  1. app has config with useUnifiedTopology: true - everything working as expected
  2. app starts randomly crashing and presents with a MongoTimeoutError matching the title of this issue, as well as the first few lines of a stack trace presented above
  3. a Google / SO search eventually brings me here
  4. I use useUnifiedTopology: false workaround suggested
  5. the MongoTimeoutError error disappears along with the crashing of the app. however, it is replaced by the deprecation warning initially. then, later, by a additional MongoNetworkError not seen in the application before setting useUnifiedTopology: false

Perhaps, as you suggest, this is correlation rather than causation. Just seemed like a smoking gun to me. At least the app is not crashing now. I'm just worried about leaving an option that is deprecated.

@mbroadst
Copy link

mbroadst commented Nov 7, 2019

@bigsee Ah! Sorry, I misread your original comment to mean that you were confirming that the v3.3.4-rc0 bug fixes resolved your issues from v3.3.3. Have you had a chance to try the new version?

@Syfon01
Copy link

Syfon01 commented Nov 8, 2019

This is my first trial on Mongodb and I'm having this error. read through the comments and changed my db to v3.3.4-rc0 and it didn't still fix the error

DeprecationWarning: 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.
MongoNetworkError: failed to connect to server [cluster0-shard-00-00-rhdve.mongodb.net:27017] on first connect [MongoNetworkError: connection 3 to cluster0-shard-00-00-rhdve.mongodb.net:27017 closed
at TLSSocket. (C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\connection.js:356:9)
at Object.onceWrapper (events.js:297:20)
at TLSSocket.emit (events.js:209:13)
at net.js:588:12
at TCP.done (_tls_wrap.js:479:7) {
name: 'MongoNetworkError',
errorLabels: [Array],
[Symbol(mongoErrorContextSymbol)]: {}
}]
at Pool. (C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\topologies\server.js:433:11)
at Pool.emit (events.js:209:13)
at C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\pool.js:562:14
at C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\pool.js:999:9
at callback (C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\connect.js:109:5)
at C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\connect.js:129:7
at Connection.errorHandler (C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\connect.js:321:5)
at Object.onceWrapper (events.js:297:20)
at Connection.emit (events.js:209:13)
at TLSSocket. (C:\laragon\www\Vue-express-mongodb\node_modules\mongodb\lib\core\connection\connection.js:354:12) {
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {}
}
TypeError: Cannot read property 'find' of undefined
at C:\laragon\www\Vue-express-mongodb\server\routes\api\posts.js:12:26

@mbroadst
Copy link

mbroadst commented Nov 8, 2019

@Syfon01 the error you are experiencing is because of some sort of configuration error, the driver is using the legacy topology and failing to make its initial connection to a cluster after a default of 10s. Please check your connection string and network configuration and verify that you are able to connect to the cluster (perhaps with the mongo shell). If you're still experiencing issues, please open a jira ticket so we can continue conversation there, and not confuse people looking at this GitHub issue

@robintom
Copy link

robintom commented Nov 10, 2019

For people who are having this issue , if you are using Mongo Atlas Cloud Instance , Please see if you are having your IP address specified correctly in the Network Access tab.

I had the issue and it seems that since my ISP gives out New IPs everytime (DHCP) i reconnect to the network , Mongo Atlas was only white listing the previously specified IP address. Hope this helps someone.

@mtn2bay
Copy link

mtn2bay commented Nov 11, 2019

update - doesn't work

I was able to solve this issue by changing the structure of my code, specifically the callback as follows:

mongoose.connect(DATABASE_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(() => {
    console.log('Connected to DB');
    app.listen({ port: PORT }, () => {
        console.log(`Server running at http://localhost:${PORT}`)
    });
}).catch(err => console.log(err));
mongoose.connect(DATABASE_URL, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true,
}, () => {
    console.log('Connected to DB');
    app.listen({ port: PORT }, () => {
        console.log(`Server running at http://localhost:${PORT}`);
    });
}).catch(err => console.log(err));

@khaledosman
Copy link

@mtn2bay that doesn't make any sense you basically used the callback function instead of the promise, the result is still one and the same, the only difference is that in the callback function you get both the connection and the error in the callback function which you're not logging instead of relying on .then and .catch, so your function is logging connected to db and starting the app either way even if there's an error. try this instead

mongoose.connect(DATABASE_URL, { 
    useNewUrlParser: true, 
    useUnifiedTopology: true,
}, (err, connection) => {
if(err) {
console.error(err)
return
}    
console.log('Connected to DB');
    app.listen({ port: PORT }, () => {
        console.log(`Server running at http://localhost:${PORT}`);
    })

@mtn2bay
Copy link

mtn2bay commented Nov 11, 2019

@khaledosman thank you for the correction. You are right, I was just ignoring the error rather than addressing it. I actually had an issue with my db path.

@ramandeep-singh-1983
Copy link

i was facing the same issue, i updated mongoose npm package from 5.6.9 to 5.7.6 and the issue vanished.

@UmerMIB
Copy link

UmerMIB commented Nov 13, 2019

For people who are having this issue , if you are using Mongo Atlas Cloud Instance , Please see if you are having your IP address specified correctly in the Network Access tab.

I had the issue and it seems that since my ISP gives out New IPs everytime (DHCP) i reconnect to the network , Mongo Atlas was only white listing the previously specified IP address. Hope this helps someone.

solved my issue
UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms at Timeout.setTimeout (C:\Users\Umer MIB\Desktop\Chatbots2\mongobot\node_modules\mongodb\lib\core\sdam\topology.js:878:9) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10) (node:4304) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:4304) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

@SoulAdor
Copy link

I do not know how that solved a problem, but I downloaded Compass and connected it to database, just copied the link. Once it got connected, the error was gone. Topology is still true in my code. Maybe it configured some options with Compass that were not right with usual mongodb installation. Now I can connect without Compass and all seems to work, just did it once in the beginning.

@nihash0608
Copy link

I got the same issue and thanks to this forum, got through by commenting useUnifiedTopology setting. However then I uncommented this setting, but now it still works just fine... quite confused...

@vkarpov15
Copy link
Collaborator

Mongoose v5.7.11 uses the newly release MongoDB driver 3.3.4, so I'm going to close this issue. If you're getting a similar error message, please open up a new issue and follow the issue template.

@Automattic Automattic locked as resolved and limited conversation to collaborators Nov 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests