Skip to content

Remove another race condition in LazyLoader - #39464

Merged
AndriySvyryd merged 1 commit into
mainfrom
LazyLoadingRace
Nov 4, 2026
Merged

AndriySvyryd merged 1 commit into
mainfrom
LazyLoadingRace

Conversation

@AndriySvyryd

Copy link
Copy Markdown
Member

The existing test caught it.

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.

🔵 Needs a closer look

Manual validation is needed because the synchronization and AsyncLocal reentrancy changes affect recursive and cross-thread Load/LoadAsync interleavings that the current stress test does not cover.

No open issues 🎉

What changed in this PR

This pull request removes a race condition in LazyLoader navigation load-state tracking and adds concurrent regression coverage.

Changes:

  • Replaces the non-thread-safe loaded-state dictionary with ConcurrentDictionary.
  • Moves loading-depth updates outside the loading-state lock in synchronous and asynchronous paths.
  • Adds multithreaded tests for initialized and uninitialized navigation states.
  • The test barriers should be made failure-safe to prevent hangs when a worker fails before reaching the second barrier.

This pull request requires changes.

File Description
test/​EFCore.Tests/​Infrastructure/​Internal/​LazyLoaderTest.cs Adds concurrent coverage for SetLoaded and IsLoaded; barrier waits need timeout/cancellation handling and peer cancellation on worker failure.
src/​EFCore/​Infrastructure/​Internal/​LazyLoader.cs Uses thread-safe loaded-state storage and adjusts loading-depth bookkeeping around the lock.

Review effort: Lite

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

main f29db88 LazyLoadingRace 4c24e8c +/-
76% 76% 0%
View coverage of the 10 most impacted files.
File main f29db88 LazyLoadingRace 4c24e8c +/-
src/EFCore.Sqli...ryRepository.cs 97% 91% -6%
src/Microsoft.D...nnectionPool.cs 100% 97% -3%
src/EFCore.Cosm...erExtensions.cs 100% 100% 0%
src/EFCore.Cosm...nsExtensions.cs 82% 82% 0%
src/EFCore.Cosm...erExtensions.cs 46% 46% 0%
src/EFCore.Cosm...peExtensions.cs 73% 73% 0%
src/EFCore.Cosm...StreamHelper.cs 87% 87% 0%
src/EFCore.Cosm...erExtensions.cs 87% 87% 0%
src/EFCore.Rela...osticsLogger.cs 98% 99% +1%
src/EFCore.Rela...CommandCache.cs 98% 100% +2%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

@AndriySvyryd
AndriySvyryd marked this pull request as ready for review October 30, 2026 03:27
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner October 30, 2026 03:27
@AndriySvyryd
AndriySvyryd merged commit 72e0f93 into main Nov 4, 2026
26 of 27 checks passed
@AndriySvyryd
AndriySvyryd deleted the LazyLoadingRace branch November 4, 2026 23:06
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants