Why does Azure CosmosDB emulator intermittently fail to respond?

110 Views Asked by At

I am developing a web server to be deployed on Azure and in the process I am using the CosmosDB emulator, locally installed on my Windows 10 machine. The problem is that the database seems to refuse to connect intermittently - let me stress this, it sometimes responds and most of the times it does not. Why is this so and what can I do to get around the problem?

  • Docker is not used, for server nor client.
  • OS is Windows 10.
  • I am using CosmosDB's MongoDB API.
  • I am using pymongo to connect synchronously (in scripts), and motor to connect asynchronously (for the web server).

Both with pymongo and motor I experience the same problem, namely the error output

pymongo.errors.ServerSelectionTimeoutError: localhost:10255: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1129), Timeout: 100.0s, Topology Description: <TopologyDescription id: 63aaec350ac3d3aa70c8bcf7, topology_type: Unknown, servers: [<ServerDescription ('localhost', 10255) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:10255: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1129)')>]>

which occurs intermittently, ie not all the time, but most of the times.

As far as I can see, the pattern is that the failure occurs for a stretch of time, and then for a stretch of time it works without error.

I am thinking that this cannot be a certificate error, because the connection sometimes does work (and because everything is on the same machine). AFAIU it looks like a timeout error, but I have tried timeouts up to 200 seconds.

Code: An example snippet of a data-import script

with open(data_filename, 'r') as infile:
    new_items = json.load(infile)

CONNECTION_STRING = os.environ.get('MONGODB_URL')
client = pymongo.MongoClient(CONNECTION_STRING)
db = client[constants.DB_NAME]
collection = db[constants.COLLECTION_CONTACTS]

with pymongo.timeout(100):
    for item in new_items:
        existing_items = collection.find(filter={'name': item['name']})
        if existing_items:
            print(f'Item exists for {existing_items[0]["name"]} with id: {[i["_id"] for i in existing_items]}')
            # continue

Other actions, such as retrieving data with motor, or listing databases with client.list_database_names(), will fail similarly.

1

There are 1 best solutions below

0
On

Client issues such as these are nearly impossible to troubleshoot on a forum because of all the variables within users' PCs. The documentation for the emulator recommends that for connectivity issues, users collect trace files and open a support ticket.

You can learn more at Troubleshoot issues when using the Azure Cosmos DB Emulator