Why is pymongo 3 giving ServerSelectionTimeoutError?

5,124 views
Skip to first unread message

Mark Stephens

unread,
Jun 27, 2015, 8:39:45 PM6/27/15
to mongod...@googlegroups.com, Mark Stephens

I'm using:
  • python 3.4.2
  • pymongo 3.0.2
  • mongolab running mongod 2.6.9
  • uwsgi 2.0.10
  • cherrypy 3.7.0
  • nginx 1.6.2

uwsgi start params:

--socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2

I setup my MongoClient ONE time:

    self.mongo_client = MongoClient('mongodb://user:p...@host.mongolab.com:port/mydb')
    self.db = self.mongo_client['mydb']

I try and save a json dict to mongo:

    result = self.db.jobs.insert_one(job_dict)

It works via a unit test that executes the same code path to mongodb. However when I execute via cherrypy and uwsgi using an HTTP POST, I get this:

pymongo.errors.ServerSelectionTimeoutError: No servers found yet

Why am I seeing this behavior when run via cherrypy and uwsgi? Is this perhaps the new thread model in pymongo 3?

update:

if I run without uWSGI and nginx by using the cherrypy built-in server, the insert_one() works.

update 1/25 4:53pm EST:

After adding some debug in pymongo, it appears that topology._update_servers() knows that the server_type = 2 for server 'myserver-a.mongolab.com'. However server_description.known_servers() has the server_type = 0 for server 'myserver.mongolab.com'

This leads to the following stack trace:

result = self.db.jobs.insert_one(job_dict)
File "/usr/local/lib/python3.4/site-packages/pymongo/collection.py", line 466, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/lib/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet

Bernie Hackett

unread,
Jun 29, 2015, 12:36:06 PM6/29/15
to mongod...@googlegroups.com, markrs...@gmail.com
Are these two machines (myserver-a and myserver) in a replica set? If so, are both of those hosts reachable from where you are running nginx?

Mark Stephens

unread,
Jul 1, 2015, 5:13:48 PM7/1/15
to mongod...@googlegroups.com, markrs...@gmail.com
This is a sandbox instance on mongolab.  I can ping both hosts from my linux box on AWS.

Bernie Hackett

unread,
Jul 1, 2015, 6:46:30 PM7/1/15
to mongod...@googlegroups.com
Are both hosts mongod instances? If so, are they in the same replica set?

On Wed, Jul 1, 2015 at 2:13 PM, Mark Stephens <markrs...@gmail.com> wrote:
This is a sandbox instance on mongolab.  I can ping both hosts from my linux box on AWS.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/7qX03P3_zKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/34c14ca9-a3b9-450a-90ad-e84b23d4d603%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mark Stephens

unread,
Jul 1, 2015, 6:55:30 PM7/1/15
to mongod...@googlegroups.com
It looks like this is just a single instance:

 

Server: 192.168.1.254

Address: 192.168.1.254#53


Non-authoritative answer:

Address: 54.92.xxx-xxx

Bernie Hackett

unread,
Jul 2, 2015, 12:11:01 PM7/2/15
to mongod...@googlegroups.com, markrs...@gmail.com
We think your problem may be related to this ticket, which we are investigating:

Mark Stephens

unread,
Jul 2, 2015, 1:23:01 PM7/2/15
to mongod...@googlegroups.com
I read that blog post a few days ago and ensured I was using a single MongoClient() instance.  I still see the same behavior.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/7qX03P3_zKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

Bernie Hackett

unread,
Jul 2, 2015, 2:44:45 PM7/2/15
to mongod...@googlegroups.com
I don't think it matters how many instances of MongoClient you create. The problem seems to have something to do process forking through the multiprocessing module. To test a hunch, could you pass connect=False when you create the MongoClient instance?

Mark Stephens

unread,
Jul 2, 2015, 2:53:39 PM7/2/15
to mongod...@googlegroups.com
That works, I'm able to persist data by calling insert_one() when passing connect=False to MongoClient()

Bernie Hackett

unread,
Jul 2, 2015, 2:58:55 PM7/2/15
to mongod...@googlegroups.com
Great. That's very useful. I suspect there is some kind of race condition between the multiprocessing module forking the process and MongoClient spawning its monitor thread.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/7qX03P3_zKM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages