Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 12, 2025

Fixes #36714

Description
The SingletonCosmosClientWrapper.Client property was using non-thread-safe lazy initialization, which caused multiple CosmosClient instances to be created under concurrent access. This led to resource contention, 100% CPU usage, and 503 errors. The fix moves CosmosClient initialization to the constructor and simplifies the conditional logic for better maintainability.

Customer impact
In high-throughput multi-threaded applications this caused resource contention, 100% CPU usage, and 503 errors. There isn't a viable workaround.

How found
Customer reported. This likely affects many EF Cosmos users to some degree but hasn't been reported before as it usually doesn't manifest itself in a reproducible manner.

Regression
No.

Testing
Test added

Risk
Low. Localized change moving from lazy initialization to constructor initialization, which is more straightforward and eliminates thread safety concerns entirely.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Cosmos SDK. GetCosmosClient is not thread safe which leads to 503s Fix thread safety in SingletonCosmosClientWrapper to prevent multiple client creation Sep 12, 2025
@Copilot Copilot AI requested a review from AndriySvyryd September 12, 2025 23:40
Copilot finished work on behalf of AndriySvyryd September 12, 2025 23:40
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Rewrite the PR summary using this template:

Fixes #{number}

**Description**
{Issue description}

**Customer impact**
{How does the reported issue affects customer's app? Are there workarounds?}

**How found**
{Was it customer reported or found during verification? How many customers are affected?}

**Regression**
{Is it a regression from a released version? Which one?}

**Testing**
{How the changes were tested}

**Risk**
{Low/Medium/High, argument why}

Copy link
Contributor Author

Copilot AI commented Sep 13, 2025

@copilot Rewrite the PR summary using this template:

...

Done. Rewrote the PR description using the requested template with sections for Description, Customer impact, How found, Regression, Testing, and Risk.

Copilot finished work on behalf of AndriySvyryd September 13, 2025 00:11
@Copilot Copilot AI requested a review from AndriySvyryd September 13, 2025 00:11
@AndriySvyryd AndriySvyryd changed the title Fix thread safety in SingletonCosmosClientWrapper to prevent multiple client creation [rc2] Fix race condition in SingletonCosmosClientWrapper.Client Sep 13, 2025
@AndriySvyryd AndriySvyryd marked this pull request as ready for review September 13, 2025 01:04
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner September 13, 2025 01:04
@Copilot Copilot AI requested a review from AndriySvyryd September 16, 2025 01:00
Copilot finished work on behalf of AndriySvyryd September 16, 2025 01:00
@AndriySvyryd
Copy link
Member

@copilot Use the default emulator URI and key in the UseCosmos calls in PartitionKeyBuilderExtensionsTest.cs and CosmosConventionSetBuilder.cs

Copy link
Contributor Author

Copilot AI commented Sep 16, 2025

@copilot Use the default emulator URI and key in the UseCosmos calls in PartitionKeyBuilderExtensionsTest.cs and CosmosConventionSetBuilder.cs

Done. Updated both files to use the default Cosmos emulator URI (https://localhost:8081) and key (C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==) in the UseCosmos calls. Commit 568b2df

Copilot finished work on behalf of AndriySvyryd September 16, 2025 05:07
@AndriySvyryd
Copy link
Member

@copilot Update Throws_for_missing_connection_info to call context.GetService<IDatabaseCreator>() inside of Assert.ThrowsAsync lambda

…ide Assert.ThrowsAsync lambda

Co-authored-by: AndriySvyryd <[email protected]>
Copy link
Contributor Author

Copilot AI commented Sep 16, 2025

@copilot Update Throws_for_missing_connection_info to call context.GetService<IDatabaseCreator>() inside of Assert.ThrowsAsync lambda

Done. Updated the test to call context.GetService<IDatabaseCreator>() inside the Assert.ThrowsAsync lambda instead of declaring it as a separate variable. This ensures the exception is caught when the service is accessed. Commit e7c3a56

@Copilot Copilot AI requested a review from AndriySvyryd September 16, 2025 06:29
Copilot finished work on behalf of AndriySvyryd September 16, 2025 06:29
@Copilot Copilot AI requested a review from roji September 16, 2025 09:16
Copilot finished work on behalf of roji September 16, 2025 09:16
@Copilot Copilot AI requested a review from roji September 16, 2025 09:23
Copilot finished work on behalf of roji September 16, 2025 09:23
Copy link
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RC2

@AndriySvyryd AndriySvyryd merged commit 0126da8 into release/10.0 Sep 17, 2025
8 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/fix-36714 branch September 17, 2025 07:29
cincuranet added a commit that referenced this pull request Sep 17, 2025
* Update dependencies from https://github.com/dotnet/dotnet build 283422
Updated Dependencies:
Microsoft.DotNet.Arcade.Sdk, Microsoft.DotNet.Build.Tasks.Templating, Microsoft.DotNet.Helix.Sdk (Version 10.0.0-beta.25464.104 -> 10.0.0-beta.25466.101)
Microsoft.Extensions.Caching.Memory, Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Abstractions, Microsoft.Extensions.Configuration.EnvironmentVariables, Microsoft.Extensions.Configuration.Json, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.DependencyModel, Microsoft.Extensions.HostFactoryResolver.Sources, Microsoft.Extensions.Logging, Microsoft.NETCore.App.Ref, Microsoft.NETCore.Platforms, System.Formats.Asn1, System.Runtime.Caching, System.Text.Encodings.Web, System.Text.Json (Version 10.0.0-rc.2.25464.104 -> 10.0.0-rc.2.25466.101)

* [rc2] Fix race condition in SingletonCosmosClientWrapper.Client (#36767)

Fixes #36714

Co-authored-by: AndriySvyryd <[email protected]>

* Fix ExecuteUpdate parameters for multiple properties with same name (#36792)

And validate against null required properties in JSON serialization.

Fixes #36791

* Stop transforming complex property access to EF.Property (#36786)

Fixes #36761

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Jiri Cincura ↹ <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: AndriySvyryd <[email protected]>
Co-authored-by: Shay Rojansky <[email protected]>
roji added a commit to roji/efcore that referenced this pull request Sep 19, 2025
* Update dependencies from https://github.com/dotnet/dotnet build 283422
Updated Dependencies:
Microsoft.DotNet.Arcade.Sdk, Microsoft.DotNet.Build.Tasks.Templating, Microsoft.DotNet.Helix.Sdk (Version 10.0.0-beta.25464.104 -> 10.0.0-beta.25466.101)
Microsoft.Extensions.Caching.Memory, Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Abstractions, Microsoft.Extensions.Configuration.EnvironmentVariables, Microsoft.Extensions.Configuration.Json, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.DependencyModel, Microsoft.Extensions.HostFactoryResolver.Sources, Microsoft.Extensions.Logging, Microsoft.NETCore.App.Ref, Microsoft.NETCore.Platforms, System.Formats.Asn1, System.Runtime.Caching, System.Text.Encodings.Web, System.Text.Json (Version 10.0.0-rc.2.25464.104 -> 10.0.0-rc.2.25466.101)

* [rc2] Fix race condition in SingletonCosmosClientWrapper.Client (dotnet#36767)

Fixes dotnet#36714

Co-authored-by: AndriySvyryd <[email protected]>

* Fix ExecuteUpdate parameters for multiple properties with same name (dotnet#36792)

And validate against null required properties in JSON serialization.

Fixes dotnet#36791

* Stop transforming complex property access to EF.Property (dotnet#36786)

Fixes dotnet#36761

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Jiri Cincura ↹ <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: AndriySvyryd <[email protected]>
Co-authored-by: Shay Rojansky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cosmos SDK. GetCosmosClient is not thread safe which leads to 503s
4 participants