Azure / azure-cosmos-dotnet-v2

Contains samples and utilities relating to the Azure Cosmos DB .NET SDK
MIT License
578 stars 837 forks source link

Question: Local call to CosmosDb Emulator does not timeout #604

Open AlexPlom opened 5 years ago

AlexPlom commented 5 years ago

Context: Local call to CosmosDb Emulator does not timeout

While testing what connectivity errors might occur during query execution, I stumbled upon a strange case, where the query is executed as such :

var query= _client.CreateDocumentQuery<Sample>(
                    UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId), queryOptions)
                    .Where(f => f.id == id).AsDocumentQuery();
Sample[] queryResult = await GetAllResultsAsync(query).ConfigureAwait(false); 

with GetAllResultsAsync() being:

private async Task<T[]> GetAllResultsAsync<T>(IDocumentQuery<T> queryAll)
        {
            var list = new List<T>();

            while (queryAll.HasMoreResults)
            {
                var docs = await queryAll.ExecuteNextAsync<T>().ConfigureAwait(false); //here we hang

                foreach (var d in docs)
                {
                    list.Add(d);
                }
            }

            return list.ToArray();
        }

And upon closing the Emulator, the request hangs and does not respond. For tests purposes, I have tried using the ConnectionPolicy for the DocumentClient as such:

var policy = new ConnectionPolicy()
            {
                RequestTimeout = new TimeSpan(0, 0, 10), //10 seconds
                RetryOptions = new RetryOptions()
                {
                    MaxRetryAttemptsOnThrottledRequests = 0,
                    MaxRetryWaitTimeInSeconds = 0
                },
                MediaRequestTimeout = new TimeSpan(0, 0, 10),
                MaxConnectionLimit = 1
            };

This is not something that hinders our process in any way, but simply trying to figure out the behavior.

Steps to reproduce: Create a DocumentClient instance and before the execution of a query, stop the emulator.

meikeric commented 5 years ago

I can confirm this is an issue.

meikeric commented 5 years ago

I did some additional investigation. If I use IDocumentClient.CreateDatabaseIfNotExistsAsync or IDocumentClient.CreateDatabaseAsync and attempt to connect to Cosmos DB local emulator but it is not installed, after 06m08s, it will finally throw "Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll". This should be almost immediate.

I am working on the Microsoft fhir server project at https://github.com/Microsoft/fhir-server/