Fix DelayPromise constructor race condition (#40356)
authorBen Adams <thundercat@illyriad.co.uk>
Mon, 10 Aug 2020 22:34:07 +0000 (23:34 +0100)
committerGitHub <noreply@github.com>
Mon, 10 Aug 2020 22:34:07 +0000 (15:34 -0700)
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs

index 3b34af0..13b4914 100644 (file)
@@ -5382,9 +5382,9 @@ namespace System.Threading.Tasks
                 if (millisecondsDelay != Timeout.Infinite) // no need to create the timer if it's an infinite timeout
                 {
                     _timer = new TimerQueueTimer(state => ((DelayPromise)state!).CompleteTimedOut(), this, (uint)millisecondsDelay, Timeout.UnsignedInfinite, flowExecutionContext: false);
-                    if (IsCanceled)
+                    if (IsCompleted)
                     {
-                        // Handle rare race condition where cancellation occurs prior to our having created and stored the timer, in which case
+                        // Handle rare race condition where completion occurs prior to our having created and stored the timer, in which case
                         // the timer won't have been cleaned up appropriately.  This call to close might race with the Cleanup call to Close,
                         // but Close is thread-safe and will be a nop if it's already been closed.
                         _timer.Close();