Hello,
I have sets of web services running on Google Cloud Run Functions (also called Cloud Functions gen 2), supporting concurrency.
With Pymongo (4.8.0), I open a connection to the Atlas using srv url like this:
uri = "mongodb+srv://myAtlas URL/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority".format(os.getenv("MONGODB_SRV"))
client = MongoClient(uri, tls=True, tlsCAFile=certifi.where(), tlsCertificateKeyFile='secret/X509-cert.pem', server_api=ServerApi('1'), tz_aware=True)
Following best practices (https://www.mongodb.com/docs/atlas/manage-connections-google-cloud/) I open the connection on top of the function, to avoid connection to be recreated upon API call. I didn’t adjust the maxIdleTimeMS settings yet, because I presume cause is elsewhere.
This works 100% for the first call (after a cold star of the cloud run functions container), but calls after can fail randomly.
In the logs, I have:
- One warning : MongoClient opened before fork. May not be entirely fork-safe, proceed with caution. See PyMongo’s documentation for details: Frequently Asked Questions - PyMongo 4.8.0 documentation
- One exception : pymongo.errors.ServerSelectionTimeoutError: No replica set members found yet, Timeout: 30s
Note : I do not use any VPC Peering or NAT, it’s going via public network.
For the warning, am I supposed to do the init of the Mongo Client in a different way?
What could be the reason of the exception? Does the connection can be recreated automatically after idle time?
Thanks