Skip to content

Fixes handling of methods Task.WaitAsync and SemaphoreSlim.WaitAsync#122

Open
michelebastione wants to merge 3 commits into
zompinc:masterfrom
michelebastione:waitasync-fixes
Open

Fixes handling of methods Task.WaitAsync and SemaphoreSlim.WaitAsync#122
michelebastione wants to merge 3 commits into
zompinc:masterfrom
michelebastione:waitasync-fixes

Conversation

@michelebastione

@michelebastione michelebastione commented Jun 20, 2026

Copy link
Copy Markdown

Description

This PR addresses two closely related bugs that caused entire method bodies being inadvertently dropped at the invocation of Task.WaitAsync or SemaphoreSlim.WaitAsync.

Both issues originated from a syntax check for the string "WaitAsync":

  1. In AsyncToSyncRewriter.ReplaceAsync the pattern matching for case MemberAccessExpressionSyntax lacked a fallback for validation purposes (when called from EndWithAsync). Hence WaitAsync calls chained at the end of a method (e.g., FooAsync().WaitAsync()) made the entire expression chain always return null, leading to them ultimately being discarded.
  2. Due to the check being based solely on the ExpressionSyntax value, there was no differentiation between Task methods, which are supposed to be dropped, and methods such as SemaphoreSlim.WaitAsync, which should be converted to SemaphoreSlim.Wait instead. As a result, synchronization primitives were being deleted from the generated code.

Changes Introduced

  1. More clearly separated the concerns of methods ReplaceAsync and EndsWithAsync. The latter now implements a fallback to a non null value for the MemberAccessExpressionSyntax pattern matching case, thus preventing the entire chain call to be dropped.
  2. Added the TryReplaceIdentifier method that uses the code block's SemanticModel for making sure that a symbol called "WaitAsync" is removed only when its containing type corresponds to System.Threading.Tasks.Task or System.Threading.Tasks.TimeProviderTaskExtensions.

fixes #119
fixes #121

@michelebastione michelebastione changed the title Fix handling of methods Task.WaitAsync and SemaphoreSlim.WaitAsync Fixes handling of methods Task.WaitAsync and SemaphoreSlim.WaitAsync Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant