-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add mechanism to clear ServiceProviderCache (#27189) #37487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add mechanism to clear ServiceProviderCache (#27189) #37487
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements a mechanism to clear the internal EF Core service provider cache to address memory leaks in testing scenarios. The implementation adds a Clear() method to ServiceProviderCache and exposes it through a static method on EntityFrameworkMetrics and an extension method on DatabaseFacade.
Changes:
- Added
Clear()method toServiceProviderCacheto clear the internal configurations dictionary - Added static
ClearServiceProviderCache()entry point (intended but incomplete) - Added
DatabaseFacadeextension method to provide a public API surface - Added unit test to verify cache clearing functionality
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/EFCore/Internal/ServiceProviderCache.cs | Adds virtual Clear() method to clear the internal _configurations dictionary |
| src/EFCore/Infrastructure/Internal/EntityFrameworkMetrics.cs | Adds using statement for ServiceProviderCache namespace (but missing the actual static method implementation) |
| src/EFCore/Infrastructure/Internal/InfrastructureExtensions.cs | Adds DatabaseFacade extension method to call EntityFrameworkMetrics.ClearServiceProviderCache() |
| test/EFCore.Tests/ServiceProviderCacheTest.cs | Adds test that verifies cache clearing using reflection to check internal state |
| /// <summary> | ||
| /// Clears the internal Entity Framework service provider cache. | ||
| /// </summary> | ||
| public static void ClearServiceProviderCache(this DatabaseFacade databaseFacade) | ||
| => EntityFrameworkMetrics.ClearServiceProviderCache(); |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ClearServiceProviderCache extension method is missing the required internal API XML documentation comment. According to EF Core coding guidelines, all members in Internal namespaces must include the standard internal API documentation comment that warns users about using internal APIs.
Co-authored-by: Copilot <[email protected]>
249ae47 to
6b86657
Compare
…om/Abde1rahman1/efcore into feature-clear-serviceProviderCache
|
@AndriySvyryd , Done. I've addressed all comments: updated the XML documentation with the concurrency warning, removed the redundant extension method as requested, and enhanced the test to verify functional behavior by asserting that a new service provider instance is created after clearing the cache |
Summary
This PR implements a "pubternal" way to clear the internal service provider cache, as requested in #27189. This allows developers to resolve memory leaks and state corruption issues, especially in heavy testing scenarios where many internal service providers are created.
Changes
Added
Clear()toServiceProviderCacheto allow clearing the internal_configurationsdictionary.Added a static
ClearServiceProviderCache()method toEntityFrameworkMetricsin theInfrastructure.Internalnamespace.Added a unit test in
ServiceProviderCacheTestto ensure the cache is effectively cleared.Tests for the changes have been added (for bug fixes / features)
Code follows the same patterns and style as existing code in this repo