question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CosmosClient initialization (BuildAndInitializeAsync/CreateAndInitializeAsync) with TokenCredential doesn't respect cancellation token

See original GitHub issue

The following code (or it’s alternative without the builder with CosmosClient/CreateAndInitializeAsync) doesn’t respect the cancellation token:

return new CosmosClientBuilder(cosmosDbUri, tokenCredential)
                        .WithConnectionModeDirect(enableTcpConnectionEndpointRediscovery: true)
                        .BuildAndInitializeAsync(new[] { (database, container) }, cancellationToken).GetAwaiter().GetResult()
                        .GetDatabase(c.Database)
                        .GetContainer(c.Container);

In order to reproduce you can use DefaultAzureCredential locally as it takes some time to find a working credential.

Expected behavior: immediate cancelation Actual behavior: cancelation after first token is returned or GetTokenAsync threw an exception SDK Version: 3.29.0 OS Version: Windows (doesn’t matter)

I found the bug in the following source file: Microsoft.Azure.Cosmos\src\Authorization\TokenCredentialCache.cs#191, method RefreshCachedTokenWithRetryHelperAsync

    this.cachedAccessToken = await this.tokenCredential.GetTokenAsync(
        requestContext: this.tokenRequestContext,
        cancellationToken: default);

The cancelation token is not propagated to this call. Only if cancellation was requested just before the call, it will be cancelled.

This bug causes health issues in my Service Fabric service since SF’s RunAsync is not able to respond properly. I am creating my own TokenCredential wrapper in order to overcome this issue.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
benjzhcommented, Aug 23, 2022

Yes @ealsur, perfectly summarized!

0reactions
ealsurcommented, Aug 23, 2022

Fair point on the try/catch, I completely missed it and you are right, the catch won’t work if there is a failure.

Let’s summarize this issue into 2 improvements:

  1. Change the signature of this method to async and await the call to Task.WhenAll: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/CosmosClient.cs#L1345-L1363

  2. Make sure the calls to this.tokenCredentialCache.GetTokensAsync receive this.cancellationToken: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos/src/Authorization/TokenCredentialCache.cs#L191-L193

Read more comments on GitHub >

github_iconTop Results From Across the Web

CosmosClient.CreateAndInitializeAsync Method
Creates a new CosmosClient with the account endpoint URI string and TokenCredential. In addition to that it initializes the client with containers provided...
Read more >
cancellationToken not being thrown when calling cosmos
I'm having a difficult time understanding why my database request is not cancelling out. I know the request is taking about 3.5 seconds....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found