Mongo DB connection via pymongo in Python not responding or hangs intermittently

I am connecting to mongoDB using pymongo MongoClient in python.
MongoDB ping command and other operations hangs or gets stuck without responding.
This issue occurs intermittently only and when the same operation is done in next second it may be working.
In below code block the first print statement works but second print statement never gets printed since the ping command is not responding.

`
from pymongo import MongoClient
client = MongoClient(mongo_url)
print(“Starting to ping”)
ping = client.admin.command(‘ping’)
print(“Completed ping”)


Note:
MongoClient version is 3.10
MongoDB Server version is 4.2
Python version is 3.10

Tried updating code as below but we are not getting any errors or exceptions and no response for infinite time.

from pymongo import MongoClient
client = MongoClient(mongo_url, waitQueueTimeoutMS=5000, socketTimeoutMS=5000,
serverSelectionTimeoutMS=5000, connectTimeoutMS=5000, maxPoolSize=100)
try:
print(“Starting to ping”)
ping = client.admin.command(‘ping’)
print(“Completed ping”)
except ConnectionFailure:
print("Server not available ")
except pymongo.errors.PyMongoError as e:
print("Pymongo error: " + str(e))
except Exception as exc:
print("Exception: " + str(exc))

1 Like

Hello, what version of pymongo are you using? If you’re using 4.8, which we just released yesterday, could you try downgrading to 4.7.3?

It seems that at some point mongo db operations freezes. Even though that particular operation including ping command never responds the very next operation may be success.
We are using pymongo version 3.10. We are facing the same issue in pymongo version 3.9 also.
We are only facing this intermittent issue in aws.

Hi, we are using pymongo version 3.10. We are facing the same issue in pymongo version 3.9 also.
It seems that at some point mongo db operations freezes. Even though that particular operation including ping command never responds the very next operation may be success.

@Shane Is there any other solutions.
It seems that at some point mongo db operations freezes. Even though that particular operation including ping command never responds but the very next operation may be success.
We are using pymongo version 3.10. We are facing the same issue in pymongo version 3.9 also.
We are only facing this intermittent issue in aws.

I can’t say without more info. Could you try calling dump_traceback when the operation hangs? Something like this where blocking_workload should be replaced with your hanging code:

import faulthandler
import threading
import time


def blocking_workload():
    time.sleep(100)


def main():
    thread = threading.Thread(target=blocking_workload)
    thread.daemon = True  # Set daemon to avoid blocking on exit.
    thread.start()
    # Print traceback of all threads on timeout.
    thread.join(3)
    if thread.is_alive():
        faulthandler.dump_traceback()


if __name__ == "__main__":
    main()

Running this script will produce output like this:

$ python print_threads_on_timeout.py
Thread 0x000000016c1ff000 (most recent call first):
  File "/Users/shane/git/mongo-python-driver/fault.py", line 7 in blocking_workload
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/threading.py", line 989 in run
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/threading.py", line 1052 in _bootstrap_inner
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/threading.py", line 1009 in _bootstrap

Current thread 0x00000001ea598c00 (most recent call first):
  File "/Users/shane/git/mongo-python-driver/fault.py", line 17 in main
  File "/Users/shane/git/mongo-python-driver/fault.py", line 21 in <module>

This will show use where the hang is happening.