13

When I start some of my services, it reports such warnings and the services stop:

/usr/lib64/python2.6/site-packages/pymongo/topology.py:75: 
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 "

However, the MongoClient has been using the parameter connect=False, as you can review the code below:

client = MongoClient(host, port, connect=False)

It is still not working. By the way, I have upgraded my pymongo version to 3.4.0. Can someone give me some suggestions?

Cheers, Kai

a small orange
  • 560
  • 2
  • 16
KOP Lee
  • 161
  • 1
  • 1
  • 7

2 Answers2

19

If you use the MongoClient for any operation that contacts the MongoDB server, then the MongoClient must create connections and background threads. Once this has happened it is no longer safe to use it in a forked subprocess. For example, this is unsafe:

client = MongoClient(connect=False)
client.admin.command('ping')  # The client now connects.
if not os.fork():
    client.admin.command('ping')  # This will print the warning.

Make sure that you aren't doing anything with the client before the fork that causes it to connect.

Better yet, don't create the client before forking at all. Create your client after the fork, in the subprocess.

A. Jesse Jiryu Davis
  • 23,641
  • 4
  • 57
  • 70
  • The code was working before I restarted all services. That is quite strange. – KOP Lee Sep 28 '17 at 02:46
  • @KOPLee - same thing here. I think it has something to do with race conditions after the introduction of proper async in python and a slew of problems it led to. – chiffa Jul 21 '20 at 12:17
  • Why is this unsafe? – Akaisteph7 Sep 13 '22 at 14:32
  • It is OK to create the client before forking, but you must not perform any operation with it or connect to a database before forking. I wish I had found the above answer earlier... – Jylpah Jan 24 '23 at 20:59
1

I had the same problem but with slightly different setup. For me it was Flask app and MongoClient was created via flask_mongoengine. Here is my answer

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124