Azure / azure-cosmos-dotnet-v3

.NET SDK for Azure Cosmos DB for the core SQL API
MIT License
745 stars 496 forks source link

Accessing or Creating database in a docker CosmosDb Emulator with the SDK hangs, no exception #4560

Closed diegosasw closed 5 months ago

diegosasw commented 5 months ago

Version 3.41.0

Describe the bug Unable to create or read databases using the SDK and CosmosDb emulator by following steps at https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-develop-emulator?tabs=docker-linux%2Ccsharp&pivots=api-nosql

To Reproduce This repo reproduces it https://github.com/diegosasw/cosmosdb-sample/tree/77d18b9b1861201b5e51003825540795ca37a7d7

  1. Spin up a CosmosDb emulator docker container
    docker run \
    --publish 8081:8081 \
    --publish 10250-10255:10250-10255 \
    --name linux-cosmosdb-emulator \
    --detach \
    mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest
  2. Download the certificate from the container
    curl -k https://localhost:8081/_explorer/emulator.pem > emulatorcert.crt
  3. Install the certificate (with elevated permissions) in the TRCA
    certutil.exe -addstore root emulatorcert.crt
  4. Clone .NET 8 console repo and run. See how it hangs. No exceptions, nothing.
    git clone https://github.com/diegosasw/cosmosdb-sample.git
    cd cosmosdb-sample
    dotnet run --project src/CosmosDbSample

Expected behavior It should create the database, be able to read it, or at least throw an exception informing on why it can't.

Actual behavior It hangs, no error, nothing.

Environment summary SDK Version: .NET 8 OS Version (e.g. Windows, Linux, MacOSX): Windows 11

Additional context More info at the above sample repository. Is there any basic .NET app using this SDK and interacting with CosmosDb docker emulator? I am new to CosmosDb and, after reading Microsoft documentation, I am unable to run a simple console app using the docker emulator.

ealsur commented 5 months ago

This is the wrong repository, we do not handle Emulator specific issues.

Please consider: https://github.com/Azure/azure-cosmos-db-emulator-docker

diegosasw commented 5 months ago

Thanks. This may be an emulator specific issue, but shouldn't the SDK throw an exception or similar and not simply hang when trying to create a database?

ealsur commented 5 months ago

@diegosasw An async application does not hang. If that is the case, then the problem is not the SDK but the .NET TaskScheduler in the machine/environment. Async operations are enqueued on the TaskScheduler and executed as threads become available.

So either the application is blocking threads, thus preventing the TaskScheduler from executing the async operations (application bug) or what you mention as a "hang" is not a proper "hang" but rather an operation that is in course, just taking longer than what you'd expect.

If you change the endpoint to a real account instead of the emulator and the problem persists, then it's probably the first one.

My hunch is that the problem is the latter. The SDK has retry semantics that are designed to provided high availability when connecting to a real service endpoint. The SDK might be retrying and never actually able to connect, you can add a CancellationToken to your operation to stop the retries.

CancellationTokenSource source = new CancellationTokenSource(TimeSpan.FromSeconds(120));
await cosmosClient.CreateDatabaseIfNotExistsAsync("foo", source.Token);

You can enable traces if you want to debug further, they should show the activity on the SDK.