Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy loading is not thread safe #35528

Open
henriquewr opened this issue Jan 25, 2025 · 0 comments · May be fixed by #35529
Open

Lazy loading is not thread safe #35528

henriquewr opened this issue Jan 25, 2025 · 0 comments · May be fixed by #35529
Labels
area-change-tracking area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-bug
Milestone

Comments

@henriquewr
Copy link

Bug description

When acessing a lazy loading property that is kinda heavy do load (a lot of data in the db), and doing it with heavy concurrency, the lazy loader fails

sometimes the lazy loading property becomes null, and then after a short period of time it gets the value
and sometimes the lazy loader throw exeptions

Your code

I unfortly cant show the exact code because it's from my job, but it is basically that


var someEntity = _dbContext.SomeDbSet.FirstOrDefault();

var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 1000 };
  Parallel.For(0, 60000, parallelOptions, i =>
  {
     var var1 =  someEntity.SomeLazyLoadICollection;
     var var2 =  someEntity.SomeLazyLoadICollection2;
     var var3 =  someEntity.SomeLazyLoadProp;
     var var4 =  someEntity.SomeLazyLoadICollection3;
     var var5 =  someEntity.SomeLazyLoadICollection4;
     var var6 =  someEntity.SomeLazyLoadICollection5;
  });

Stack traces


Verbose output


EF Core version

9.0.1

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9.0

Operating system

Windows 10

IDE

Visual Studio 2022 17.12.4

henriquewr added a commit to henriquewr/efcore that referenced this issue Jan 25, 2025
henriquewr added a commit to henriquewr/efcore that referenced this issue Jan 25, 2025
 - Added sync for lazy loading

Fixes dotnet#35528
@henriquewr henriquewr linked a pull request Jan 25, 2025 that will close this issue
@AndriySvyryd AndriySvyryd self-assigned this Jan 25, 2025
@AndriySvyryd AndriySvyryd added this to the 10.0.0 milestone Jan 25, 2025
@AndriySvyryd AndriySvyryd added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution labels Jan 29, 2025
@AndriySvyryd AndriySvyryd removed their assignment Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants