Fix an assertion failure in the thread pool's QueueUserWorkItemCallbackBase (#25482)
authorKoundinya Veluri <kouvel@users.noreply.github.com>
Fri, 28 Jun 2019 20:08:22 +0000 (13:08 -0700)
committerStephen Toub <stoub@microsoft.com>
Fri, 28 Jun 2019 20:08:22 +0000 (16:08 -0400)
Fixes https://github.com/dotnet/coreclr/issues/25242
- The issue could be that there is no memory barrier (or in the wrong place) on the thread reading the value, and an old value could be cached and read

src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs

index 064227b..dca22ab 100644 (file)
@@ -770,11 +770,12 @@ namespace System.Threading
     internal abstract class QueueUserWorkItemCallbackBase : IThreadPoolWorkItem
     {
 #if DEBUG
-        private volatile int executed;
+        private int executed;
 
         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1821:RemoveEmptyFinalizers")]
         ~QueueUserWorkItemCallbackBase()
         {
+            Interlocked.MemoryBarrier(); // ensure that an old cached value is not read below
             Debug.Assert(
                 executed != 0, "A QueueUserWorkItemCallback was never called!");
         }