Skip to content

[release/10.0] Fix GetRelationalModel race condition - #39423

Merged
AndriySvyryd merged 1 commit into
release/10.0from
backport/39216-release-10.0
Nov 2, 2026
Merged

AndriySvyryd merged 1 commit into
release/10.0from
backport/39216-release-10.0

Conversation

Copilot AI commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39214
Backports #39216

Description
When multiple threads make the first call to GetRelationalModel for 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 throws ModelNotFinalized. 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 with InvalidOperationException during 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.

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 AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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

@AndriySvyryd
AndriySvyryd marked this pull request as ready for review October 27, 2026 21:00
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner October 27, 2026 21:00

@artl93 artl93 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

User reported, concurrency issue, regression. Approved.

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.

5 participants