mongoose: 4.7.1 => 4.7.2 : MongoError connection timed out

When testing mongoose@4.7.2 / mongodb@2.2.12, I get:

MongoError: connection 2 to XXXXXX timed out at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:29:11) at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:186:20) at Socket.g (events.js:286:16) at emitNone (events.js:86:13) at Socket.emit (events.js:185:7) at Socket._onTimeout (net.js:333:8) at tryOnTimeout (timers.js:224:11) at Timer.listOnTimeout (timers.js:198:5)’ } 'connection 2 to XXXXXX timed out

this does not happen with mongoose@4.7.1 / mongodb@2.2.11.

More details:

  • query: aggregate with allowDiskUse(true).read(‘secondaryPreferred’).cursor()
  • timing: the error happens about 35s after sending the query (before first results are received)
  • node version: v6.3.0
  • npm version: 3.10.9
  • connect options:
{
 "socketOptions": {
	"socketTimeoutMS": 240000,
	"keepAlive": 10000,
	"connectTimeoutMS" : 30000
} 
  • query:
[
	{ $match: {
			_id: { $lt: oid },
			signature: { $exists: true }
		}
	},
	{ $group: { 
			_id: { myid: '$myid', signature: '$signature' },
			count: { $sum:  1 },
			myid: { $first: '$myid' },
			docs: { $push: { _id: '$_id', name: '$name' } }
		}
	},
	{ $match: {
			count: { $gt : 1 }
		}
	}
]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 44 (2 by maintainers)

Most upvoted comments

The issue has been unresolved for some time now. What should we do to fix this? At some point I would like to update to the recent version but I can only do that when this has been resolved.

I was also having the same issue with mongo 3.6.2 and mongoose 4.9.2 I fixed it by passing additional connection parameters

MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000

Not sure if this helps anyone, but in our case we noticed that we were passing in our timeout params (socketTimeoutMS etc) in through options.server. This was wrong! We are using a replica set and as such our options should have as below:

In our case the application actually needed the ability to connect to either a replica or normal server, so we did:

const serverOptions = {
  poolSize: 100,
  socketOptions: {
    socketTimeoutMS: 60000
  }
};

mongoose.createConnection(dbpath, {
  server: serverOptions,
  replset: serverOptions
});

Hope that helps someone!

I think there may be more to this issue than meets the eye. I’ve had to revert to 4.4.20 to completely stop these aggregate timeouts occurring under all circumstances. 4.7.x and 4.6.x both had bogus very quick timeouts under some circumstances of multiple concurrent executions. I skipped 4.5 and went straight back to a known good version (4.4.20) which we are using in production, and that has completely solved the problem, but TBH I’m still mystified by what is happening. I’m sorry I don’t have any further data to add, but I’m not sure if it is solely a problem with the version of the native mongodb driver that mongoose is using, or if there’s more than one contributory factor at work.

I’m experiencing the same problem, mongoose@4.7.4, mongodb@2.2.16

  • setting socketTimeoutMS to higher values or 0 makes no difference
  • setting connectTimeoutMS to higher values or 0 makes no difference
  • no “close” or “reconnected” event is fired, only a “timeout”, which results in a Mongoose error in the callback

Rolling back to mongoose@4.7.1 / mongodb@2.2.11 solves the issue, i.e., does not cause timeouts

From mongodb@2.2.11 to 2.2.12 the timeout event listener changed from “once” to “on”, maybe this has something to do with it?

Or should we post this as an issue in the mongodb native driver?

In case it helps anyone, I experienced this issue when parallelizing 2 brutal find() queries (using Promise pattern).

Turning

Promise.all([
  col1.find({longQuery: true}),
  col2.find({longQuery: true})
])
.spread(function(result1, result2) {
  //stuff
});

into a sequential approach

col1.find({longQuery: true})
.then(function(result1) {
  return Promise.all([
    result1,
    col2.find({longQuery: true})
  ]);
})
.spread(function(result1, result2) {
  //stuff
});

Made it work

Same here with:

├─┬ mongoose@4.9.5
│ ├─┬ mongodb@2.2.25
│ │ ├── mongodb-core@2.1.9

mongo version @ 3.2.11

on AWS EC2.

Any update/workaround? Would keepAlive: true have any positive effect?

mongodb@2.2.17 updated to mongodb-core@2.1.3 which includes a fix for monitoring operations timing out and generating a timeout event on running operations @1284917

Soon Mongoose updates to mongodb@2.2.19, I will give it a try.

@silentjohnny Getting the same issue with mongoose@4.7.4

For those experiencing the intermittent errors, I had those too on mongoose-4.13.11 with mongodb-2.2.35 mongodb-core-2.1.19 in a particular environment. Various queries at different points in our testsuite, though mostly testing one particular part of our app which has heavier queries and does do things in parallel IIRC, were all getting this error message. And the whole test suite does not even take 15s to run, so there’s no way I should have been hitting the built-in default 30s timeouts in the first place:

     MongoError: connection 0 to localhost:27017 timed out
      at Function.MongoError.create (node_modules/mongoose/node_modules/mongodb-core/lib/error.js:29:11)
      at Socket.<anonymous> (node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:200:20)
      at Socket._onTimeout (net.js:448:8)

This only happened in our CI test build server. I couldn’t reproduce it on my dev machine.

(In my own system (private repo, this is for my own reference in case ever need to dig this up again), this error shows up at least in build numbers are 358, 356, 355, 352).

After turning on useMongoClient: true, the tests seem to pass reliably in the CI environment so far. I have had three successful runs with just that change whereas many prior sequential runs fail due to the weird timeout error.

I think @thenitai @steve-p-com @adrienbaron were experiencing the weird bogus timeouts too. I think the error was happening to you guys before 4.11 was out which even brought useMongoClient: true as an option. Can you tell if that made the difference for your workloads?

@cyrilchapon thought it was working for me but your sequential approach did not solve my issue 😕

it is intermittent… sometimes it works, sometimes it times out…

Hello Everyone, I am using mlab to host my database. I get this error Mongo DB connectionerror: {[MongoError: connection 0 to ds155841.mlab.com:xxxxx timed out’}

I’m seeing this issue too on my server with Mongoose 4.7.7, MongoDB 3.4.1 and Node 4.7.2. When memory usage is high on my server, timeouts seems to occur randomly in series:

CosWebsite-27 MongoError: connection 42 to 127.0.0.1:27017 timed out
CosWebsite-27     at Function.MongoError.create (/home/cos/cos/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:29:11)
CosWebsite-27     at Socket.<anonymous> (/home/cos/cos/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:186:20)
CosWebsite-27     at Socket.g (events.js:260:16)
CosWebsite-27     at emitNone (events.js:67:13)
CosWebsite-27     at Socket.emit (events.js:166:7)
CosWebsite-27     at Socket.wrapped (/home/cos/cos/node_modules/newrelic/lib/transaction/tracer/index.js:183:28)
CosWebsite-27     at Socket.wrappedEmit [as emit] (/home/cos/cos/node_modules/newrelic/lib/transaction/tracer/index.js:220:46)
CosWebsite-27     at Socket._onTimeout (net.js:333:8)
CosWebsite-27     at _runOnTimeout (timers.js:537:11)
CosWebsite-27     at _makeTimerTimeout (timers.js:528:3)
CosWebsite-27     at Timer.unrefTimeout (timers.js:597:5)
... times 6
CosWebsite-27 GET /clans/compas-c-r-98QP9J2G/members 500 10569.300 ms - 9893
CosWebsite-27 GET /players/1000-99JQVQ9VU 500 12388.484 ms - 9849
CosWebsite-27 GET /players/R0YUPPRR/profile 500 8204.622 ms - 9857
CosWebsite-27 GET /players/UG8YJUJY/profile 500 4622.819 ms - 9857
CosWebsite-27 GET /clans/next-state-P8RYGQYV 500 11526.859 ms - 9861
CosWebsite-27 GET /clans/YY2CCUVV 500 6755.380 ms - 9817

As you can see the requests none of the request lasted more than 12s. My mongoose connector is set up this way:

const mongoDB = {
	uri: "mongodb://127.0.0.1:27017/XXX",
	options: {
		host: '127.0.0.1',
		port: '27017',
		database: "XXX",
		compression: false,
		server: {
			poolSize: 5,
			auto_reconnect: true,
			socketOptions: {
				socketTimeoutMS: 0,
				connectTimeoutMS: 0
			}
		},
		promiseLibrary: Promise
	}
};

Any idea what could be going wrong?

mongoose@4.7.7 seems to have solved the timeouts for me. @steve-p-com like you are suggesting, the timeouts in 4.7.2 were indeed bogus and caused error events being emitted on the wrong connection, this was solved by mongodb-core@2.1.6 and so mongoose@4.7.7 should work fine in your case. Did you run a test with that version?

Haven’t tested 4.7.9 or 4.8.0 yet, maybe mongodb@2.2.22 introduced a regression, which would explain the issue @flosky is experiencing. Does that timeout also happen on mongoose@4.7.7?

I’m certain the timeouts are often bogus. In one scenario I was looking at I started four simultaneous aggregate.exec operations which take around 30 seconds. One timed out in 8 seconds, one timed out in 20 seconds, but the other two completed normally in 30 seconds. Repeating caused a different timeout order, but always a similar result. I suspect timeouts are incorrectly being set on the wrong connections, or are not being removed when an operation completes so that they fire unexpectedly on a subsequent exec. Something like that anyway. Unfortunately I’ve no time at the moment to pursue it.

Having the same problem with mongoose@4.7.9 and mongodb@2.2.22. Timeouts are really weird. This one is pretty short (around 5s) and very reproducible but other queries take longer than 5s and do not time out