-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Fixed lazy loading thread safety #35529
base: main
Are you sure you want to change the base?
Conversation
- Added sync for lazy loading Fixes dotnet#35528
@dotnet-policy-service agree |
var willWait = true; | ||
TaskCompletionSource taskCompletionSource; | ||
|
||
lock (_isLoadingLock) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in the last commit I already did a thread sync in the ConcurrentDictionary
I changed to use the normal Dictionary, and instead of GetOrAdd i use a slightly faster approach
changing that makes it about 15% faster
Added tests for lazy loading thread safety
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
@henriquewr Could you explain what the issue in the original code was? It seems that it made some incorrect assumption, but I don't see what it was exactly |
Oh, I see. While it prevented a second thread from loading it didn't make it wait for the first thread to finish. |
@AndriySvyryd Exacly that what i was going to say |
fixed async lazy loading
|
||
if (exists) | ||
{ | ||
if (isLoadingValue.Depth.Value == 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting optimization. It definitely warrants a comment on how it works as the next person to read this code will be very confused.
@roji @cincuranet What's your opinion of this usage of AsyncLocal
?
Added sync for lazy loading
Fixes #35528