Fix flaky ServicePropertyDiscoveryConventionTest.Finds_service_properties_in_hierarchy#38128
Merged
AndriySvyryd merged 1 commit intomainfrom Apr 17, 2026
Merged
Conversation
The test was flaky because ServiceProviderCacheTest.Service_provider_cache_can_be_cleared called Clear() on the global ServiceProviderCache.Instance singleton, which could race with the hierarchy test when running in parallel. When the cache was cleared between the two using blocks, the second context got a new service provider with an empty in-memory database, resulting in 0 loaded entries. Two fixes applied: 1. Changed Service_provider_cache_can_be_cleared to use a local ServiceProviderCache instance instead of the global singleton (root cause fix) 2. Added a shared InMemoryDatabaseRoot to ServicePropertiesContext so the in-memory database survives service provider changes (defensive fix) Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/c7ac9a3c-4847-4884-8223-c969d57ac007 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
AndriySvyryd
April 17, 2026 01:25
View session
There was a problem hiding this comment.
Pull request overview
Fixes a flaky test caused by concurrent clearing of the global ServiceProviderCache.Instance, which could force recreation of a service provider and unintentionally lose the InMemory store between two DbContext instances in the same test.
Changes:
- Updated
Service_provider_cache_can_be_clearedto use an isolatednew ServiceProviderCache()instance rather than the global singleton. - Made
ServicePropertiesContextuse a sharedInMemoryDatabaseRootso the InMemory store survives service provider recreation during the test.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/EFCore.Tests/ServiceProviderCacheTest.cs | Avoids clearing the global cache by using a local ServiceProviderCache instance in the Clear() behavior test. |
| test/EFCore.Tests/Metadata/Conventions/ServicePropertyDiscoveryConventionTest.cs | Uses a shared InMemoryDatabaseRoot to keep InMemory data stable across multiple contexts even if the internal service provider changes. |
roji
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Service_provider_cache_can_be_clearedcallsClear()on the globalServiceProviderCache.Instancesingleton. When running concurrently withFinds_service_properties_in_hierarchy, the cache gets cleared between the twousingblocks, causing the second context to resolve a new service provider with an empty in-memory store → 0 entries loaded.Service_provider_cache_can_be_clearednow uses a localnew ServiceProviderCache()— it's testingClear()behavior, not the global singletonServicePropertiesContextnow uses a sharedInMemoryDatabaseRootso the store survives service provider recreation