question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

All usages of async/await syntax should use "ConfigureAwait"

See original GitHub issue

Throughout Elsa, because it is shared/reusable code which could be hosted in any one of a number of environments, any place where the await keyword is used, it should also use the .ConfigureAwait(false) method.

The reasons and problem by not doing this is described in depth in this article, but it could be summarised as: “If you are writing reusable/library code and wish to use async/await, then always use .ConfigureAwait(false) with your awaits. This will prevent accidental thread-deadlocking when the library is consumed in certain environments.”

// Bad example:
await DoTheThingAsync();

// Good example:
await DoTheThingAsync().ConfigureAwait(false);

Consequences/symptom

As eluded to above, in some environments - such as those which have a concept of “a UI thread”, not using ConfigureAwait can cause an unwanted deadlock on the main (UI) thread. That will manifest as the whole application hanging and entering an indefinitely deadlocked state. Unfortunately it is not always possible to ‘fix’ this problem from the consuming app. It is far better to ‘fix it’ from the library.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
hankovichcommented, Oct 27, 2020

Who am I to argue with Mr. Toub? 😊

I’m 99% percent sure you’re right and nobody will set it to true, so I’m ok to follow yagni here.

The next question is, how do you force developers to use ConfigureAwait everywhere? I see two options:

  1. Voodoo magic provided by Fody (https://github.com/Fody/ConfigureAwait) that will just automatically add it to all awaits.
  2. Play with verbosity of the inspection CA2007.

I guess which option you will choose 😊

1reaction
sfmskywalkercommented, Oct 26, 2020

Well, consider me schooled. Turns out I knew even less than I already thought I did. 😅

@craigfowler Thank you for providing that excellent resource! I haven’t read everything in detail yet, but after skimming over it (including the referenced GitHub issues at the end) I still agree that it’s best if Elsa follows this pattern.

@hankovich I’m curious to hear your thoughts too after reading Stephen’s FAQ. We can of course consider following CSharpFunctionExtension’s example of reducing the ConfigureAwait(false) noise with an extension. Although I don’t see any benefits to allowing the flag to be configurable, given Stephen’s explanation on why it’s pretty much useless to ever set this to true.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async, Await, and ConfigureAwait – Oh My!
By default, when you use async/await, it will resume on the original thread that started the request. However, if another long-running process ...
Read more >
A deep dive into ConfigureAwait
In 99% of the cases, you should use ConfigureAwait(false ). In .NET Framework by default the Task execution will continue on the captured...
Read more >
c# - Why is writing ConfigureAwait(false) on every line with ...
As a general rule, yes. ConfigureAwait(false) should be used for every await unless the method needs its context.
Read more >
ConfigureAwait FAQ - .NET Blog
ConfigureAwait(false) involves a task that's already completed by the time it's awaited (which is actually incredibly common), then the ...
Read more >
Nested Async Calls and ConfigureAwait(false) - Vasil Kosturski
An article on the internals of nested async/await calls. You'll see a very detailed workflow diagram with all the execution steps.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found