Repository navigation
[release/10.0] Fix GetRelationalModel race condition - #39423
Merged
Merged
Conversation
Recheck the relational model annotation if its factory has already been removed. Add a deterministic regression test for concurrent first access.\n\nFixes #39214\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot CLI created this pull request from a session on behalf of
AndriySvyryd
October 27, 2026 01:17
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The race-condition fix is narrowly scoped, preserves existing behavior for uninitialized models, and includes deterministic regression coverage.
No open issues 🎉
What changed in this PR
This pull request fixes a race condition during lazy relational-model initialization that could incorrectly throw ModelNotFinalized during concurrent first use.
Changes:
- Rechecks the relational-model annotation when the factory has already been removed.
- Adds deterministic regression coverage for concurrent initialization.
- Preserves the existing exception for genuinely uninitialized models.
| File | Description |
|---|---|
test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs |
Adds regression coverage for concurrent relational-model initialization. |
src/EFCore.Relational/Extensions/RelationalModelExtensions.cs |
Rechecks the completed relational model before throwing ModelNotFinalized. |
Review effort: Lite
cincuranet
approved these changes
Oct 30, 2026
artl93
approved these changes
Oct 30, 2026
artl93
left a comment
Member
There was a problem hiding this comment.
User reported, concurrency issue, regression. Approved.
This was referenced Nov 14, 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.
Fixes #39214
Backports #39216
Description
When multiple threads make the first call to
GetRelationalModelfor the same model, one thread can finish building the relational model and remove its factory after another thread has observed that the model is missing. The second thread then incorrectly throwsModelNotFinalized. This race was introduced when relational-model creation became lazy in EF 9 (#33650). The fix rechecks the model annotation if the factory is already gone, while retaining the existing error for models that are genuinely uninitialized.Customer impact
Concurrent first queries,
SaveChanges, or migrations may fail withInvalidOperationExceptionduring application startup. No data corruption occurs. A workaround is to serialize the first model use, but it is not readily discoverable.How found
3 User reports on EF Core 10.0.*
Regression
Yes, from EF Core 8, introduced in #33650.
Testing
Test added
Risk
Very low. Functional behavior is not affected.