Treat missing .ConfigureAwait(false)
as errors (and fix)
#633
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.
I enabled the new .NET Analyzer for the project and then used an Editor Config rule to treat CA2007 as an error. This diagnostic is for any awaited task that does not have an explicit trailing
.ConfigureAwait(false)
call, an annoying but very necessary configuration for asynchronous .NET library code (such as OmniSharp).The bugs caused by these missing calls are strange. In the case of PowerShell Editor Services, an LSP server using OmniSharp and powering the PowerShell Extension for VS Code, it showed up as a hang when the server executed user code that used objects from
System.Windows.Forms
.This is because that .NET library sets its own synchronization context, which is exactly where
ConfigureAwait(continueOnCapturedContext: false)
comes into play. The default value istrue
which roughly means that the awaited tasks will only run on their own context. So when the context is changed (because ofSystem.Windows.Forms
or other code introducing a new synchronization context) the task will never be continued, resulting in this hang. By configuring this tofalse
we allow the tasks to continue regardless of the new context.See this .NET blog post for more details: https://devblogs.microsoft.com/dotnet/configureawait-faq/
Note that elsewhere in the codebase we've been very careful to set it to false, but we've not been perfect. Treating this diagnostic as an error will allow us to be as perfect as possible about it.