Fix to allow entering cooperative GC mode when the thread is in a forbid-suspend...
authorKoundinya Veluri <kouvel@users.noreply.github.com>
Wed, 23 Sep 2020 17:08:45 +0000 (13:08 -0400)
committerGitHub <noreply@github.com>
Wed, 23 Sep 2020 17:08:45 +0000 (10:08 -0700)
- Followup to https://github.com/dotnet/runtime/pull/40060
- In short timing windows if a thread is placed in a pending-suspend-for-debugger state while in a forbid-suspend-for-debugger region, from the above PR it would not suspend for the debugger but it was missed that it can also get stuck in a perpetual spin-wait while entering cooperative GC mode. This causes the thread to not suspend for the debugger (VS times out after waiting) and the thread can only progress by unmarking it for debugger suspension.
- Fixed to break the spin-wait by checking whether the thread is in the forbid region

Fix for https://github.com/dotnet/runtime/issues/42375 in master

src/coreclr/src/vm/threadsuspend.cpp

index 3da47ca..b941d60 100644 (file)
@@ -2269,8 +2269,9 @@ void Thread::RareDisablePreemptiveGC()
     // Note IsGCInProgress is also true for say Pause (anywhere SuspendEE happens) and GCThread is the
     // thread that did the Pause. While in Pause if another thread attempts Rev/Pinvoke it should get inside the following and
     // block until resume
-    if ((GCHeapUtilities::IsGCInProgress()  && (this != ThreadSuspend::GetSuspensionThread())) ||
-        (m_State & (TS_DebugSuspendPending | TS_StackCrawlNeeded)))
+    if ((GCHeapUtilities::IsGCInProgress() && (this != ThreadSuspend::GetSuspensionThread())) ||
+        ((m_State & TS_DebugSuspendPending) && !IsInForbidSuspendForDebuggerRegion()) ||
+        (m_State & TS_StackCrawlNeeded))
     {
         STRESS_LOG1(LF_SYNC, LL_INFO1000, "RareDisablePreemptiveGC: entering. Thread state = %x\n", m_State.Load());
 
@@ -2361,7 +2362,8 @@ void Thread::RareDisablePreemptiveGC()
             // thread while in this loop.  This happens if you use the COM+
             // debugger to suspend this thread and then release it.
             if (! ((GCHeapUtilities::IsGCInProgress() && (this != ThreadSuspend::GetSuspensionThread())) ||
-                    (m_State & (TS_DebugSuspendPending | TS_StackCrawlNeeded))) )
+                    ((m_State & TS_DebugSuspendPending) && !IsInForbidSuspendForDebuggerRegion()) ||
+                    (m_State & TS_StackCrawlNeeded)) )
             {
                 break;
             }