You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is the reason of the behaviour in #44 .
The synchronous versions of the public methods (e.g. EncryptStream) are just calls to the async version of that method (e.g. EncryptStreamAsync) suffixed with .Wait().
You should never do this because it causes deadlocks, let me try to explain why:
Wait will synchronously block until the task completes. So the current thread is literally blocked waiting for the task to complete. When the code reaches an await statement, it returns an uncompleted task. When this Tasks completes, it wants to synchronize with the request context from where the Task is initiated. It will wait until this thread comes available. Because the Wait() method blocks the current thread, it will never become available: deadlock.
As a general rule, you should use "async all the way down"; that is, don't block on async code.
What you should do is make the synchronous methods truly synchronous, or, remove them so the calling clients are forced to use the async methods.
There is another option though, but it is more or less a hack: suffix every async call with ConfigureAwait(false). This will prevent the task from synchronizing back with the current request context, avoiding a deadlock.
Thanks for pointing that out, I didn't realise Wait was quite that bad. I'll probably depreciate the sync rather than converting them with the aim of removing them further down the road.
This issue is the reason of the behaviour in #44 .
The synchronous versions of the public methods (e.g.
EncryptStream
) are just calls to the async version of that method (e.g.EncryptStreamAsync
) suffixed with.Wait()
.Example:
EncryptStreamAsync(inputStream, outputStream, publicKeyStreams, armor, withIntegrityCheck).Wait();
You should never do this because it causes deadlocks, let me try to explain why:
Wait
will synchronously block until the task completes. So the current thread is literally blocked waiting for the task to complete. When the code reaches anawait
statement, it returns an uncompleted task. When this Tasks completes, it wants to synchronize with the request context from where the Task is initiated. It will wait until this thread comes available. Because theWait()
method blocks the current thread, it will never become available: deadlock.As a general rule, you should use "async all the way down"; that is, don't block on async code.
What you should do is make the synchronous methods truly synchronous, or, remove them so the calling clients are forced to use the async methods.
There is another option though, but it is more or less a hack: suffix every async call with
ConfigureAwait(false)
. This will prevent the task from synchronizing back with the current request context, avoiding a deadlock.Here is a good read about this topic.
The text was updated successfully, but these errors were encountered: