Skip to content
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

Add analyzer to force ConfigureAwait(false) on all awaits #31132

Closed
JamesNK opened this issue Apr 17, 2019 · 14 comments
Closed

Add analyzer to force ConfigureAwait(false) on all awaits #31132

JamesNK opened this issue Apr 17, 2019 · 14 comments
Labels
area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework
Milestone

Comments

@JamesNK
Copy link
Member

JamesNK commented Apr 17, 2019

We don't add ConfigureAwait(false) to ASP.NET Core code because there is no sync context. Microsoft.Extensions.* code could execute in client apps. Not using ConfigureAwait(false) can create deadlocks when a user mixes sync with async, e.g. dotnet/extensions#999.

Consider introducing an analyzer to enforce ConfigureAwait(false) everywhere. A quick search of this repos source code displays many non-test awaits that don't follow best practice.

I just had some good success using https://www.nuget.org/packages/ConfigureAwaitChecker.Analyzer/

@rynowak
Copy link
Member

rynowak commented Apr 17, 2019

don't follow best practice.

And like most "best practices" it's only correct like 90% of the time 😆

@JamesNK
Copy link
Member Author

JamesNK commented Apr 17, 2019

That's what pragma suppress is for.

@rynowak
Copy link
Member

rynowak commented Apr 17, 2019

Or use an explicit .ConfigureAwait(true). I just wanted to make a pithy remark in general. "Best practice" usually hides some nuanced guidance that needs to be understood.

@JamesNK
Copy link
Member Author

JamesNK commented Apr 17, 2019

Ha. Best practice is code for I don't want to explain why we want it 😋

https://stackoverflow.com/a/27851460/11829

@analogrelay
Copy link
Contributor

@ericstj Does dotnet/runtime already have an analyzer like this? I see some references to it. If so, this might be much easier for the components moving to dotnet/runtime.

@ericstj
Copy link
Member

ericstj commented Mar 10, 2020

We enable CA2007 /cc @stephentoub

@stephentoub
Copy link
Member

stephentoub commented Mar 10, 2020

@sharwell
Copy link
Member

sharwell commented Mar 10, 2020

CA2007 is outdated (among other things, doesn't support ValueTask). You'll want to use the vs-threading one instead, and disable CA2007 so you aren't getting duplicate diagnostics and fixes.

@stephentoub
Copy link
Member

stephentoub commented Mar 10, 2020

CA2007 is outdated (among other things, doesn't support ValueTask). You'll want to use the vs-threading one instead, and disable CA2007 so you aren't getting duplicate diagnostics and fixes.

The sheer noise from vs-threading analyzers was prohibitive.

If CA2007 is outdated, it should be fixed. Or if it's not being invested in because the vs-threading analyzers are being incorporated into these packages as a replacement, we'll wait for those before making any changes.

At present, CA2007 is what exists for us and our customers.

@analogrelay
Copy link
Contributor

This is mostly done now. The dotnet/runtime repo already does this. We should turn on CA2007 in this repo as well though.

@Anipik Anipik transferred this issue from dotnet/extensions Mar 22, 2021
@javiercn
Copy link
Member

@JamesNK is there anything for us to do here?

Should we close this? If not, do you want to bring it for someone to pick it up in the DoI?

@JamesNK
Copy link
Member Author

JamesNK commented Mar 23, 2021

I don't know what Andrew ment when he said this is mostly done. I don't think we've done anything 😬

Basically I think we should turn on CA2007 for all our libraries that can run outside of ASP.NET Core. We're writing code without ConfigureAwait(false) because we're assuming that there is never a sync context, but some of our libraries can be used in WinForms or WPF. That's created bugs (linked in initial comment).

We should come up with a rule to apply the analyzer setting across the repo. Perhaps all projects that are:

  • Published as NuGet packages
  • IsAspNetCoreApp is false

Note that there are some projects where the rule will be turned off. For example, the .NET SignalR client has a different strategy to avoid sync context issues (@BrennanConroy can you confirm, its been a couple of years since I looked at the .NET SignalR client code).

tldr; DoI should pick this up

@javiercn javiercn added the area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework label Mar 23, 2021
@BrennanConroy
Copy link
Member

I don't know what Andrew ment when he said this is mostly done.

I think Andrew meant we had added .ConfigureAwait(false) to most of the Extensions code.

For example, the .NET SignalR client has a different strategy to avoid sync context issues

Yeah, we have the weird ForceAsync method. It's very error prone though as you need to remember to add it to any new public top-level methods. We might want to discuss possibly changing that.

@pranavkm
Copy link
Contributor

I'm going to consider this resolved based on #39946.

@pranavkm pranavkm added this to the 7.0-preview3 milestone Feb 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-infrastructure Includes: MSBuild projects/targets, build scripts, CI, Installers and shared framework
Projects
None yet
Development

No branches or pull requests

9 participants