mongodb / flask-pymongo

PyMongo support for Flask applications
BSD 2-Clause "Simplified" License
721 stars 175 forks source link

ServerSelectionTimeoutError: No servers found yet with pymongo 3.1 #66

Closed dkiser closed 8 years ago

dkiser commented 8 years ago

I received the following warning/error messages below for Flask app using pymongo. Monkey patching https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/mongo_client.py#L81 to connect=False fixes the issue. Also see http://stackoverflow.com/questions/31030307/why-is-pymongo-3-giving-serverselectiontimeouterror.

warning message

/usr/lib64/python2.7/site-packages/pymongo/topology.py:74: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#using-pymongo-with-multiprocessing>
  "MongoClient opened before fork. Create MongoClient "

traceback

  File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 1654, in distinct
    with self._socket_for_reads() as (sock_info, slave_ok):
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 720, in _socket_for_reads
    with self._get_socket(read_preference) as sock_info:
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 684, in _get_socket
    server = self._get_topology().select_server(selector)
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 141, in select_server
    address))
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 117, in select_servers
    self._error_message(selector))
ServerSelectionTimeoutError: No servers found yet

pip freeze versions

Flask-PyMongo==0.4.0
pymongo==3.1

Can Flask-Pymongo we smart enough to detect >1 cores/threads and call the __init__ of PyMongo with an appropriate connect argument?

hbldh commented 8 years ago

@dkiser I don't think detection of >1 cores/threads is enough to handle this; I think that the PyMongo crowd would have implemented that instead of adding warnings in the api documentation (https://api.mongodb.org/python/3.1/faq.html#multiprocessing). Seems to me like adding a configuration option for this would be a better way to go.

dkiser commented 8 years ago

@hbldh sounds good to me.

hbldh commented 8 years ago

@dkiser Tests didn't pass on Travis at first, but when I added MONGO_CONNECT=False to all test cases it did.

@ranman Might be that this setting is required for correct handling of PyMongo 3.1 on Travis CI?

sibelius commented 8 years ago

any progress on this?

hut8 commented 8 years ago

@dkiser Out of curiosity, what app server are you using? I am using uwsgi. I just spent like two hours with a bug that I think is the same. Mine never throws an exception but instead just hangs, and this is what I get in the logs.

announcing my loyalty to the Emperor...
/srv/venv/lib/python3.4/site-packages/pymongo/topology.py:74: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#using-pymongo-with-multiprocessing>
  "MongoClient opened before fork. Create MongoClient "
[pid: 15145|app: 0|req: 1/2]  1.2.3.4 () {46 vars in 753 bytes} [Sun Jan 24 01:54:21 2016] GET /stats => generated 39 bytes in 30023 msecs (HTTP/1.1 500) 2 headers in 90 bytes (1 switches on core 0)

There's a timeout at 30 seconds. I can connect just fine using exactly the same stack/virtualenv (minus the app server of course) using a Flask-Script shell (https://flask-script.readthedocs.org/en/latest/#shell).

My solution was a one line fix; just add lazy-apps = true to my uwsgi INI file. That makes the workers fork then load the application.

dkiser commented 8 years ago

@hut8 its been almost 3 months, so that is about 10 tech years ;) I believe I was using the same setup with uwsgi, but it failed immediately for me. We ended up just downgrading to pymong 2.8 in our Docker containers in the meantime.

hbldh commented 8 years ago

@hut8 @dkiser I have been using gunicorn and also CherryPy web server, both of them most often as reverse proxy web servers. I am using my fork since I sent my pull request, #67, and have not encountered ant problems yet.

A new pull request, #71, has been created with the same intent as this one, so I would not say that this issue is dead ...

ranman commented 8 years ago

I'll merge #67 after some docs updates

On Sun, Jan 24, 2016 at 2:18 PM, Henrik Blidh notifications@github.com wrote:

@hut8 https://github.com/hut8 @dkiser https://github.com/dkiser I have been using gunicorn http://gunicorn.org/ and also CherryPy http://www.cherrypy.org/ web server, both of them most often as reverse proxy web servers. I am using my fork since I sent my pull request, #67 https://github.com/dcrosta/flask-pymongo/pull/67, and have not encountered ant problems yet.

A new pull request, #71 https://github.com/dcrosta/flask-pymongo/pull/71, has been created with the same intent as this one, so I would not say that this issue is dead ...

— Reply to this email directly or view it on GitHub https://github.com/dcrosta/flask-pymongo/issues/66#issuecomment-174346705 .