Fix CancellationTokenSource_CancelAsync_AllCallbacksInvoked test (#82153)
authorStephen Toub <stoub@microsoft.com>
Wed, 15 Feb 2023 16:13:50 +0000 (11:13 -0500)
committerGitHub <noreply@github.com>
Wed, 15 Feb 2023 16:13:50 +0000 (11:13 -0500)
If the task internally queued by CancelAsync hasn't started yet by the time we block waiting for it, Wait could inline it and end up running it on the current thread.

src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs

index b1be8a7..4d2c89b 100644 (file)
@@ -1787,7 +1787,9 @@ namespace System.Threading.Tasks.Tests
             Task t = cts.CancelAsync();
             Assert.True(cts.IsCancellationRequested);
 
-            t.Wait(); // synchronously block to ensure this thread isn't reused
+            ((IAsyncResult)t).AsyncWaitHandle.WaitOne(); // synchronously block without inlining to ensure this thread isn't reused
+            t.Wait(); // propagate any exceptions
+
             Assert.Equal(Iters * (Iters + 1) / 2, sum);
         }