Mongoose startSession() hangs

328 views
Skip to first unread message

Lahiru Chandima

unread,
Oct 7, 2019, 11:22:35 PM10/7/19
to mongodb-user

Hi,


I am using mongoose to connect to my Mongodb Atlas cluster through my nodejs server.

There is a certain operation which is done as a transaction. Mongoose needs mongoose.startSession() to be called to start a transaction. Very infrequently, this mongoose.startSession() call hangs indefinitely. There is no certain way to reproduce this.


log.info('starting session');
const mongoSession = await mongoose.startSession();
log.info('session started');

In above code, starting session. gets logged, but session started doesn't get logged when issue occurs.

I connect to the db like below:


const dburl = 'mongodb+srv://myuser:mypas...@myapp.mongodb.net/mydb?retryWrites=true&w=majority';
mongoose.connect(dburl, {useNewUrlParser: true}, err => {
    if (err) {
        log.warn('Error occurred when connecting to database. ' + err);
    }
});

What could be the reason for this? Could this be due to something wrong with the database? Is there any way I can further troubleshoot this?


Thanks in advance

Kevin Adistambha

unread,
Oct 10, 2019, 1:55:49 AM10/10/19
to mongodb-user

Hi Lahiru,

Some quick questions:

  1. Do you see any error message? If not, is it possible to catch the promise rejection?
  2. What do you mean by “no certain way to reproduce this”? Does it mean that most of the time the code works without issues, and sometimes it just doesn’t work? Is there any pattern you can think of?
  3. In the event that it “hangs”, the database connection was established correctly, right?
  4. What is your Mongoose version, and what is your Atlas deployment type (M0, M2, etc.)

Best regards,
Kevin

Lahiru Chandima

unread,
Oct 10, 2019, 2:05:55 AM10/10/19
to mongod...@googlegroups.com
Hi Kevin,
Please find my responses inline.

Thanks


On Thu, Oct 10, 2019 at 11:25 AM 'Kevin Adistambha' via mongodb-user <mongod...@googlegroups.com> wrote:

Hi Lahiru,

Some quick questions:

  1. Do you see any error message? If not, is it possible to catch the promise rejection?
No. No promise rejection occurs. Promise just doesn't resolve or get rejected.
  1. What do you mean by “no certain way to reproduce this”? Does it mean that most of the time the code works without issues, and sometimes it just doesn’t work? Is there any pattern you can think of?
Most of the time the code works, and only hangs very rarely. I found that there had been a primary node restart around the last time this issue occurred, so there can be a relationship with node failovers.
  1. In the event that it “hangs”, the database connection was established correctly, right?
Yes. The nodejs server establishes the connection to the db at startup. Issue occurred well after the startup, after many successful requests to the db.
  1. What is your Mongoose version, and what is your Atlas deployment type (M0, M2, etc.)
M2 cluster with  mongodb Version 4.0.12

Best regards,
Kevin

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/53e3886c-79b2-4833-89df-45bf0ea79aa4%40googlegroups.com.


--
Lahiru Chandima

Kevin Adistambha

unread,
Oct 11, 2019, 1:05:56 AM10/11/19
to mongodb-user

Hi Lahiru,

I found that there had been a primary node restart around the last time this issue occurred, so there can be a relationship with node failovers.

This might be a hint. In your connection string:

const dburl = ‘mongodb+srv://myuser:mypas...@myapp.mongodb.net/mydb?retryWrites=true&w=majority‘;

I noticed that you don’t have the replicaSet option set (see Connection String URI Format). It’s possible that the stall happens because it just doesn’t know that it’s connected to a replica set and thus it doesn’t know that it needs to handle failovers.

Could you try adding the parameter replicaSet=<your replica set name> to the connection string and try again? Note that in Atlas M10 and above you have a “Test Failover” button that you can use to test your app for handling failovers.

Best regards,
Kevin

Lahiru Chandima

unread,
Oct 11, 2019, 1:27:00 AM10/11/19
to mongod...@googlegroups.com
Hi Kevin,

Thanks for the response.

I got the url from the connection instructions from the mongodb atlas console, so it is very unlikely that it doesn't contain all the information. Also, the mongodb+srv option in the url indicates to lookup DNS TXT records for  replicaSet and  authSource options. I checked the TXT record of the domain in my url and it includes the replicaSet option: ( authSource=admin&replicaSet=myapp-shard-0 )

Do you think it would be ok to report this to mongodb as a bug?

Thanks

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.


--
Lahiru Chandima

Kevin Adistambha

unread,
Oct 11, 2019, 1:42:01 AM10/11/19
to mongodb-user

Hi Lahiru,

Ah you’re right. In that case it should be able to pick up that it’s a replica set and act accordingly during failovers. That is, unless something in Mongoose is preventing the failovers from being detected/operated correctly. I think it’s very unlikely this is a bug from Atlas/MongoDB side. What is your Mongoose version? And have you tried the “Test Failover” button to see if you can reproduce this reliably?

Best regards,
Kevin

Lahiru Chandima

unread,
Oct 11, 2019, 1:55:52 AM10/11/19
to mongod...@googlegroups.com
Hi Kevin,

I am using mongoose 5.6.9. I will try to update it to latest version and check. Unfortunately am on a M2 cluster and I don't have the "Test Failover" button.

Thanks

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.


--
Lahiru Chandima
Reply all
Reply to author
Forward
0 new messages