Repository navigation
Fix GetRelationalModel race condition - #39216
Merged
AndriySvyryd merged 1 commit intoOct 14, 2026
Merged
Conversation
…concurrently - GetRelationalModel read the RelationalModel and RelationalModelFactory annotations without synchronization. - If another thread created the relational model and removed the factory between the two reads, it threw ModelNotFinalized although the model was initialized. - When the factory is missing, read the RelationalModel annotation again before throwing. - Add a regression test that simulates the interleaving deterministically. Fixes dotnet#39214
Contributor
Author
|
@dotnet-policy-service agree |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix preserves existing paths and the regression test accurately exercises the reported race.
0 open findings
What changed in this PR
Fixes a first-use race in lazy relational-model initialization.
Changes:
- Re-reads the relational model before reporting an unfinalized model.
- Adds a deterministic regression test for the concurrent interleaving.
| File | Description |
|---|---|
src/EFCore.Relational/Extensions/RelationalModelExtensions.cs |
Handles factory removal caused by concurrent initialization. |
test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs |
Tests the race deterministically. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
AndriySvyryd
approved these changes
Oct 14, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
Merged
7 of 11 tasks
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.
Fixes #39214
GetRelationalModelreads theRelationalModelruntime annotation and, when it is missing, theRelationalModelFactoryannotation, with no synchronization between the two reads. When two threads make the first call for the same model at once, this can happen:RelationalModeland getsnull.RelationalModeland removesRelationalModelFactory.RelationalModelFactory, getsnull, and throwsModelNotFinalized, although the relational model is built.This change reads
RelationalModelagain when the factory is missing, and only throws if it is still missing. Under the lock, the creating thread addsRelationalModelbefore it removes the factory, so a thread that finds the factory gone also finds the model. The fast path and the locked creation path are unchanged.The race appears on the first concurrent use of a shared model. Examples are
Migrator.HasPendingModelChangeson the cached design-time model when several contexts of one type migrate in parallel, and the first queries orSaveChangescalls on the runtime model. The same fix covers compiled models. A compiled model is a static instance shared by all service providers. Its factory is either generated intoInitialize()(models generated for NativeAOT) or added byRelationalModelRuntimeInitializer, and in both cases it is consumed through this method.The new test in
RelationalModelTestuses aRuntimeModelsubclass that overridesFindRuntimeAnnotation. Right after the outer call finds noRelationalModel, the override completes a nested first call, which reproduces the interleaving deterministically on one thread. Without the change the test fails withModelNotFinalized, and with the change it passes. Local runs on macOS (arm64), rebased on main: Debug and Release builds with 0 warnings; EFCore.ApiBaseline.Tests passed (no public API change); EFCore.Relational.Tests 1520 passed, 1 skipped; EFCore.Sqlite.FunctionalTests 38228 passed, 286 skipped.I filed #39214; its "Suggested fix" section proposes this change, and @AndriySvyryd approved the contribution there. This is a regression from EF 8, introduced with the lazy relational model in EF 9 (#33650). If you'd consider servicing it, I'm happy to open a port to
release/10.0.