Search code examples
pythonmultithreadingpymongoreplicaset

pymongo: What to do if monitor thread is dead?


For some reason ReplicaSet's Monitor appeared dead when refresh was scheduled.

I've got following Traceback in a call to find_one():

  File "pymongo/collection.py", line 604, in find_one
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
  File "pymongo/cursor.py", line 904, in next
    if len(self.__data) or self._refresh():
  File "pymongo/cursor.py", line 848, in _refresh
    self.__uuid_subtype))
  File "pymongo/cursor.py", line 805, in __send_message
    client.disconnect()
  File "pymongo/mongo_replica_set_client.py", line 1255, in disconnect
    self.__schedule_refresh()
  File "pymongo/mongo_replica_set_client.py", line 1067, in __schedule_refresh
    self.__monitor.schedule_refresh()
  File "pymongo/mongo_replica_set_client.py", line 295, in schedule_refresh
    "Monitor thread is dead: Perhaps started before a fork?")

I studied code a bit and found out that Monitor.monitor() contains:

  # RSC has been collected or there
  # was an unexpected error.
  except:
      break

Which means whatever bad happened, I will never find out what was it.

So, what should I do, if I catch InvalidOperation("Monitor thread is dead: Perhaps started before a fork?")

Is there some nice way to restart Monitor instance?

(I use flask-pymongo with pymongo.version 2.6.2)


Solution

  • I'm not sure the exact cause of your problem, but it sounds like a bug I fixed in PyMongo 2.7, issue PYTHON-549, "recreate monitors". Please upgrade.