Handle the case when the SamplingProfiler thread is terminated prematurely
authorAndrew Au <andrewau@microsoft.com>
Wed, 8 May 2019 00:23:42 +0000 (17:23 -0700)
committerAndrew Au <cshung@gmail.com>
Wed, 8 May 2019 14:09:06 +0000 (07:09 -0700)
src/vm/sampleprofiler.cpp

index 4b8e8b5..8fb9769 100644 (file)
@@ -113,16 +113,23 @@ void SampleProfiler::Disable()
         return;
     }
 
-    // Reset the event before shutdown.
-    s_threadShutdownEvent.Reset();
+    // If g_fProcessDetach is true, the sampling profiler thread probably got
+    // ripped because someone called ExitProcess(). This check is an attempt 
+    // to recognize that case and avoid waiting for the (already dead) sampling
+    // profiler thread to tell us it is terminated.
+    if (!g_fProcessDetach)
+    {
+        // Reset the event before shutdown.
+        s_threadShutdownEvent.Reset();
 
-    // The sampling thread will watch this value and exit
-    // when profiling is disabled.
-    s_profilingEnabled = false;
+        // The sampling thread will watch this value and exit
+        // when profiling is disabled.
+        s_profilingEnabled = false;
 
-    // Wait for the sampling thread to clean itself up.
-    s_threadShutdownEvent.Wait(INFINITE, FALSE /* bAlertable */);
-    s_threadShutdownEvent.CloseEvent();
+        // Wait for the sampling thread to clean itself up.
+        s_threadShutdownEvent.Wait(INFINITE, FALSE /* bAlertable */);
+        s_threadShutdownEvent.CloseEvent();
+    }
 
     if(s_timePeriodIsSet)
     {