Repository navigation
Fix hang in AsyncLazy<T> when value factory synchronously blocks for UI thread - #3
Merged
Merged
Conversation
...before first yielding await.
|
Hi Andrew Arnott (@AArnott), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
Member
Author
Contributor
|
"In general, this pattern of invoking 3rd party code outside one's own private lock is a very good idea to avoid deadlocks." - a good piece of advise for the threading cookbook. |
Ilya Biryukov (IlyaBiryukov)
approved these changes
Oct 17, 2016
| InlineResumable resumableAwaiter = null; | ||
| lock (this.syncObject) | ||
| { | ||
| // Note that if multiple threads hit GetValueAsync() before |
Member
Author
There was a problem hiding this comment.
Delete this obsolete code comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the value factory passed to the constructor of an AsyncLazy tries to switch to the UI thread while inside a JTF.Run, and if the UI thread itself is in a JTF.Run and then calls AsyncLazy.GetValueAsync on that same instance, a deadlock occurs because the value factory is invoked within the AsyncLazy's own private lock on the background thread -- the same lock that the UI thread then blocks on such that it is unwilling to help the background thread complete the transition to the UI thread.
This fixes that deadlock by allowing the AsyncLazy to invoke the value factory outside its own private lock. In general, this pattern of invoking 3rd party code outside one's own private lock is a very good idea to avoid deadlocks.
To make this possible, AsyncLazy needs a JoinableTask immediately when the first caller claims the right to execute the value factory so that other threads can join it. Yet the only way to obtain a JoinableTask instance is to first invoke a delegate that represents the joinable task, so that must happen inside the lock obtained by the first caller. A new
InlineResumabletype is introduced to allow the value factory to yield but resumed inline so that we don't have to wait for a threadpool thread or the UI thread to schedule the real value factory later.A few new tests for AsyncLazy are added to demonstrate the hangs are fixed.
Fixes devdiv bug 278339