Connection to Atlas server falls over every 1-5 weeks with "pymongo.errors.ServerSelectionTimeoutError: No replica set members match selector "Primary()" traceback message

Hi all,

I’m currently working on a project that uses a Flask App running in a Docker container hosted on Elastic Beanstalk. The API falls over every 1-5 weeks with a “No replica set members match selector “Primary()”” error. I’m unable to reproduce this problem as I’m not sure what’s triggering it.

I’m using a connection string in the following format;


I don’t think this is the issue, as a similar issue I found on stack overflow suggests that it’s due to the way the replica sets are set up in Atlas.

Is this a known issue and is there any documentation on troubleshooting this on atlas?

Many Thanks,


I have the same error, did you manage to fix this?

What cluster type, M0, M2 …?

See Database daily restart/disconnection - #3 by Stennie

It’s an M10. I’ve tried specifying the replicaSet name in the URL but I’m still getting the error

My issue was due to the original developer not using uWSGI correctly. The solution for me was to add the --lazy-apps option to uWSGI. If you don’t add --lazy-apps, it will interfere with pymongo’s threads (by copying the address space of the parent thread).

You could also try to alter your connection string. Try adding a &replicaSet=yourreplicasetsname option to the end of your connection string. You can also try readPreference=PrimaryPreffered, though this will only work for read operations. Any multiple collection writes would not work with this, at least on Atlas.

1 Like

I should add that fork() launches a new process, not a thread.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.