Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See lots of clr!CLRSemaphore::Wait in call stack

We see lots of callstack like the below, may I know what's conditions \situation would happens this?

OS Thread Id: 0x48654 (559)
Current frame: ntdll!NtWaitForSingleObject+0xa
Child-SP         RetAddr          Caller, Callee
00000020a76cf480 00007fffd4ea1118 KERNELBASE!WaitForSingleObjectEx+0x94, calling ntdll!NtWaitForSingleObject
00000020a76cf520 00007fffce50ce66 clr!CLRSemaphore::Wait+0x8a, calling kernel32!WaitForSingleObjectEx
00000020a76cf5e0 00007fffce50d247 clr!ThreadpoolMgr::UnfairSemaphore::Wait+0x109, calling clr!CLRSemaphore::Wait
00000020a76cf620 00007fffce50d330 clr!ThreadpoolMgr::WorkerThreadStart+0x1b9, calling clr!ThreadpoolMgr::UnfairSemaphore::Wait
00000020a76cf6c0 00007fffce5de8b6 clr!Thread::intermediateThreadProc+0x7d
00000020a76cfb40 00007fffce5de89f clr!Thread::intermediateThreadProc+0x66, calling clr!_chkstk
00000020a76cfb80 00007fffd60613d2 kernel32!BaseThreadInitThunk+0x22
00000020a76cfbb0 00007fffd7be5454 ntdll!RtlUserThreadStart+0x34
OS Thread Id: 0x3bd4c (560)
Current frame: ntdll!NtWaitForSingleObject+0xa
Child-SP         RetAddr          Caller, Callee
00000020a774e910 00007fffd4ea1118 KERNELBASE!WaitForSingleObjectEx+0x94, calling ntdll!NtWaitForSingleObject
00000020a774e9b0 00007fffce50ce66 clr!CLRSemaphore::Wait+0x8a, calling kernel32!WaitForSingleObjectEx
00000020a774ea70 00007fffce50d247 clr!ThreadpoolMgr::UnfairSemaphore::Wait+0x109, calling clr!CLRSemaphore::Wait
00000020a774eab0 00007fffce50d330 clr!ThreadpoolMgr::WorkerThreadStart+0x1b9, calling clr!ThreadpoolMgr::UnfairSemaphore::Wait
00000020a774eb50 00007fffce5de8b6 clr!Thread::intermediateThreadProc+0x7d
00000020a774ec30 00007fffd7c00c75 ntdll!RtlpLowFragHeapAllocFromContext+0x355, calling ntdll!memset
like image 620
Jason Avatar asked Mar 11 '23 10:03

Jason


1 Answers

As mentioned by @Harry Johnston in the comments already, these are worker threads of a thread pool that have nothing to do.

The following example can be used to replicate such stacks. It will create 12 threadpool worker threads and when the debugger breaks, they are all in idle state as seen by you.

The code is based on Microsoft's Fibunacci threadpool example:

using System.Diagnostics;
using System.Threading;

public class Fibonacci
{
    public void ThreadPoolCallback(object threadContext)
    {
        FibOfN = Calculate(N);
        DoneEvent.Set();
    }

    public int Calculate(int n)
    {
        if (n <= 1) return n;
        return Calculate(n - 1) + Calculate(n - 2);
    }

    public int N { get; set; }
    public int FibOfN { get; private set; }
    public ManualResetEvent DoneEvent { get; set; }
}

public class ClrSemaphoreWaitDemo
{
    static void Main()
    {
        const int numberOfTasks = 12;
        var doneEvents = new ManualResetEvent[numberOfTasks];
        var fibArray = new Fibonacci[numberOfTasks];
        ThreadPool.SetMaxThreads(numberOfTasks, numberOfTasks);
        ThreadPool.SetMinThreads(numberOfTasks, numberOfTasks);

        for (int i = 0; i < numberOfTasks; i++)
        {
            doneEvents[i] = new ManualResetEvent(false);
            fibArray[i] = new Fibonacci {N= 4, DoneEvent= doneEvents[i]};
            ThreadPool.QueueUserWorkItem(fibArray[i].ThreadPoolCallback, i);
        }

        WaitHandle.WaitAll(doneEvents);
        Debug.WriteLine("Now run .symfix; .reload; .loadby sos clr; !threads; !threads; !findstack clr!CLRSemaphore::Wait");
        Debugger.Break();
    }
}

When the debugger breaks, run the following command:

.symfix; .reload; .loadby sos clr; !threads; !threads; !findstack clr!CLRSemaphore::Wait
like image 79
Thomas Weller Avatar answered Mar 24 '23 06:03

Thomas Weller