-
Notifications
You must be signed in to change notification settings - Fork 88
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
Rewriting Async API in ImageExport #321
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module Plotly.NET.ImageExport.AsyncHelper | ||
|
||
open System.Threading | ||
open System.Threading.Tasks | ||
|
||
(* | ||
|
||
This is a workaround to avoid deadlocks | ||
|
||
https://medium.com/rubrikkgroup/understanding-async-avoiding-deadlocks-e41f8f2c6f5d | ||
|
||
TL;DR in many cases, for example, GUI apps, SynchronizationContext | ||
is overriden to *post* the executing code on the initial (UI) thread. For example, | ||
consider this code | ||
|
||
public async Task OnClick1() | ||
{ | ||
var chart = ...; | ||
var base64 = ImageExport.toBase64PNGStringAsync()(chart).Result; | ||
myButton.Text = base64; | ||
} | ||
|
||
Here we have an async method. Normally you should use await and not use .Result, but | ||
assume for some reason the sync version is used. What happens under the hood is, | ||
|
||
public async Task OnClick1() | ||
{ | ||
var chart = ...; | ||
var task = ImageExport.toBase64PNGStringAsync()(chart); | ||
task.ContinueWith(() => | ||
UIThread.Schedule(() => | ||
myButton.Text = Result; | ||
) | ||
); | ||
task.Wait(); | ||
} | ||
|
||
(this is pseudo-code) | ||
|
||
So basically, we set the task to wait until it finishes. However, part of it being | ||
finished is to actually execute the code with button.Text = .... The waiting happens | ||
on the UI thread, exactly on the same thread as where we're waiting for it to do | ||
another job! | ||
|
||
That's not the only place we potentially deadlock by using fake synchronous functions. | ||
The reason why it happens, is because frameworks (or actually anyone) override | ||
SynchronizationContext. In GUI and game development it's very useful to keep UI logic | ||
on one thread. But our rendering does not ever callback to it, we're independent of | ||
where the logic actually happens. | ||
|
||
That's why what we do is we set the synchronization context to null, do the job, and | ||
then restore it. It is a workaround, because it doesn't have to work everywhere and | ||
independently. But it will work for most cases. | ||
|
||
When will it also break? For example, if we decide to take in some callback as a para- | ||
meter, which potentially accesses the UI thread (or whatever). In Unity, for instance, | ||
you can only access Unity API from the main thread. So our fake synchronous function | ||
will crash in the end, because due to the overriden (by us) sync context, the callback | ||
will be executed in some random thread (as opposed to being posted back to the UI one). | ||
|
||
However, our solution should work in most cases. | ||
|
||
Credit to [@DaZombieKiller](https://github.com/DaZombieKiller) for helping. | ||
|
||
*) | ||
|
||
let runSync job input = | ||
let current = SynchronizationContext.Current | ||
SynchronizationContext.SetSynchronizationContext null | ||
try | ||
job input | ||
finally | ||
SynchronizationContext.SetSynchronizationContext current | ||
|
||
let taskSync (task : Task<'a>) = task |> runSync (fun t -> t.Result) | ||
|
||
let taskSyncUnit (task : Task) = task |> runSync (fun t -> t.Wait()) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
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 a great idea, as we can at least test if the render engine finishes this way. could you add this for the other image formats as well? maybe rename the tests to
Chart.toBase64PNGStringAsync terminates
etc