From d9d7259019fd8ab62faad1c1f6546a1ecf360fa2 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 14 Feb 2023 23:41:02 -0500 Subject: [PATCH] Fix CancellationTokenSource_CancelAsync_AllCallbacksInvoked test 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. --- .../System.Threading.Tasks/tests/CancellationTokenTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs b/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs index b1be8a744fe323..4d2c89ba14db57 100644 --- a/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs +++ b/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs @@ -1787,7 +1787,9 @@ public static void CancellationTokenSource_CancelAsync_AllCallbacksInvoked() 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); }