Does VB.NET observe cancellation immediately after a synchronously completed await?
0 reputation · 02 Dec 2025, 13:12 UTC
Cancellation observation timing in VB.NET async methods
When an async method in VB.NET contains multiple await points and a CancellationToken is cancelled after the first awaited task completes synchronously, the runtime may not observe the cancellation until the next await point. This creates a window where the continuation executes despite the token being cancelled.
The goal is to determine whether the VB.NET state machine inserts a cancellation check immediately after a synchronously completed await, or if the check is deferred until the next asynchronous suspension point. This behavior is documented as implementation-dependent and may vary across .NET runtime versions.
Specific questions
- Does the VB.NET compiler generate a
ThrowIfCancellationRequestedcall after a synchronously completed await? - Can the continuation between two await points run to completion even when the token is already cancelled?
- Which .NET runtime versions exhibit a change in this observation behavior?