Skip to content

Fix GetRelationalModel race condition - #39216

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
mesutk:fix/relational-model-first-touch-race
Oct 14, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
mesutk:fix/relational-model-first-touch-race

Conversation

@mesutk

@mesutk mesutk commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39214

GetRelationalModel reads the RelationalModel runtime annotation and, when it is missing, the RelationalModelFactory annotation, with no synchronization between the two reads. When two threads make the first call for the same model at once, this can happen:

  1. Thread B reads RelationalModel and gets null.
  2. Thread A completes the first call: it stores RelationalModel and removes RelationalModelFactory.
  3. Thread B reads RelationalModelFactory, gets null, and throws ModelNotFinalized, although the relational model is built.

This change reads RelationalModel again when the factory is missing, and only throws if it is still missing. Under the lock, the creating thread adds RelationalModel before 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.HasPendingModelChanges on the cached design-time model when several contexts of one type migrate in parallel, and the first queries or SaveChanges calls 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 into Initialize() (models generated for NativeAOT) or added by RelationalModelRuntimeInitializer, and in both cases it is consumed through this method.

The new test in RelationalModelTest uses a RuntimeModel subclass that overrides FindRuntimeAnnotation. Right after the outer call finds no RelationalModel, the override completes a nested first call, which reproduces the interleaving deterministically on one thread. Without the change the test fails with ModelNotFinalized, 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.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

…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
@mesutk
mesutk requested a review from a team as a code owner October 9, 2026 23:02
@mesutk

mesutk commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

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 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 AndriySvyryd 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.

Thanks for your contribution!

@AndriySvyryd AndriySvyryd changed the title Fix GetRelationalModel throwing when the relational model is created concurrently Fix GetRelationalModel race condition Oct 14, 2026
@AndriySvyryd
AndriySvyryd merged commit 791bc2c into dotnet:main Oct 14, 2026
20 of 21 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
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.

GetRelationalModel throws "The model must be finalized" when two threads make the first call at once (check-then-act on RelationalModelFactory)

3 participants