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 and chain ConfigureAwait(false) on all awaits to allow blocking on async methods without deadlocking #456

Merged
merged 2 commits into from
Mar 7, 2025

Conversation

dhsu-atlas
Copy link
Contributor

@dhsu-atlas dhsu-atlas commented Mar 7, 2025

For general-purpose library code, adding ConfigureAwait(false) for every await allows .NET Framework applications that have a SynchronizationContext to call async code synchronously without deadlocking. The 'false' argument to ConfigureAwait allows the continuation to complete on any available thread in the pool and not just the original one. Since the context of the application is unknown to the library it can't use it anyway. To prevent future changes from reintroducing await's without a ConfigureAwait(false), the .editorconfig has been updated to treat these as errors and EnableNETAnalyzers added to the csproj. Tests are excluded from this rule as they are not expected to have a SynchronizationContext.

How this affects different applications:

  • No impact on .NET Core apps. Async or synchronous calling is supported.
  • No impact on .NET Framework apps that do not rely on a SynchronizationContext. Async or synchronous calling is supported.
  • .NET Framework apps using SynchronizationContext (ASP.NET, WPF, Winforms) will no longer deadlock on synchronous calls as long as all await'ed async calls include ConfigureAwait(false).

Reference: https://devblogs.microsoft.com/dotnet/configureawait-faq/#when-should-i-use-configureawait(false)

… on async calls without deadlocking in .NET Framework applications

For general-purpose library code, adding ConfigureAwait(false) for every await allows .NET Framework applications that have a SynchronizationContext to call async code synchronously without deadlocking. The 'false' argument to ConfigureAwait allows the continuation to complete on any available thread in the pool and not just the original one. Since the context of the application is unknown to the library it can't use it anyway. To prevent future changes from reintroducing await's without a ConfigureAwait(false), the .editorconfig has been updated to treat these as errors and EnableNETAnalyzers added to the csproj. Tests are excluded from this rule as they are not expected to have a SynchronizationContext.

How does this affect calling applications:

* No impact on .NET Core apps. Async or synchronous calling is supported.
* No impact on .NET Framework apps that do not rely on a SynchronizationContext. Async or synchronous calling is supported.
* .NET Framework apps using SynchronizationContext (ASP.NET, WPF, Winforms) will no longer deadlock on synchronous calls as long as all await'ed async calls include ConfigureAwait(false).

Reference: https://devblogs.microsoft.com/dotnet/configureawait-faq/#when-should-i-use-configureawait(false)
@dhsu-atlas dhsu-atlas requested a review from phnx47 as a code owner March 7, 2025 00:54
@dhsu-atlas dhsu-atlas changed the title Add and chain ConfigureAwait(false) on all awaits to support blocking… Add and chain ConfigureAwait(false) on all awaits to allow blocking on async methods without deadlocking Mar 7, 2025
Copy link

codecov bot commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 58.62069% with 12 lines in your changes missing coverage. Please review.

Project coverage is 64.79%. Comparing base (33da63b) to head (14f0892).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/ReadOnlyDapperRepository.Find.Join.cs 16.66% 5 Missing ⚠️
src/ReadOnlyDapperRepository.FindById.Join.cs 33.33% 4 Missing ⚠️
src/DapperRepository.ExecuteJoinQuery.cs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #456   +/-   ##
=======================================
  Coverage   64.79%   64.79%           
=======================================
  Files          59       59           
  Lines        1909     1909           
  Branches      376      376           
=======================================
  Hits         1237     1237           
  Misses        567      567           
  Partials      105      105           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@phnx47 phnx47 merged commit 3e793fb into phnx47:main Mar 7, 2025
6 of 7 checks passed
@dhsu-atlas dhsu-atlas deleted the feat/add-configureawait branch March 7, 2025 03:04
@phnx47
Copy link
Owner

phnx47 commented Mar 7, 2025

Thank you!

@phnx47
Copy link
Owner

phnx47 commented Mar 7, 2025

Published v1.28.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants