debug application hung while creating a thread using beginthreadex
I encounter a strange problem on site that our application get hung while starting. I get the crashdump generated by custom on site, and it shows that the main thread was stuck by beginthreadex api. Here's the callstack:
0:006> kv
ChildEBP
0161ebb8 ntdll!KiFastSystemCallRet (FPO: [0,0,0])
0161ebbc ntdll!ZwWaitForSingleObject+0xc (FPO: [3,0,0])
0161ebf8 ntdll!RtlpWaitOnCriticalSection+0x1a3 (FPO: [2,7,4])
0161ec18 ntdll!RtlEnterCriticalSection+0xa8 (FPO: [1,1,0])
0161ec4c ntdll!LdrLockLoaderLock+0x133 (FPO: [SEH])
0161ecc8开发者_如何学Go ntdll!LdrGetDllHandleEx+0x94 (FPO: [SEH])
0161ece4 ntdll!LdrGetDllHandle+0x18 (FPO: [4,0,0])
0161ed30 kernel32!GetModuleHandleForUnicodeString+0x20 (FPO: [SEH])
0161f1a8 kernel32!BasepGetModuleHandleExW+0x17f (FPO: [SEH])
0161f1c0 kernel32!GetModuleHandleW+0x29 (FPO: [1,0,0])
0161f1cc kernel32!GetModuleHandleA+0x2d (FPO: [1,0,0])
0161f204 msvcr80!initptd+0x17
0161f224 msvcr80!beginthreadex+0x56
0161f248 ZQCommonStlp!ZQ::common::NativeThread::NativeThread+0x59 (FPO: [Non-Fpo]) (CONV: thiscall)
...
I get nothing from this callstack but just know the main thread was stuck while creating a new thread. Is this a system problem?
Are you spinning up this thread in DllMain (or some DLL initialization)? It seems to be deadlocked on the infamous loader lock. See the following docs for details of what's allowed (very little) in DllMain:
- http://msdn.microsoft.com/en-us/windows/hardware/gg487379.aspx
In this particular case, the runtime function _initptd()
is calling GetModuleHandle("KERNEL32.DLL")
.
For reference, here's a number of links about the loader lock that might be helpful, whether or not you have a DllMain in the mix:
The docs for DllMain, which has some stern warnings: http://msdn.microsoft.com/en-us/library/ms682583.aspx
And a link-fest from Raymond Chen's "Old New Thing":
- Some reasons not to do anything scary in your DllMain
- Another reason not to do anything scary in your DllMain: Inadvertent deadlock
- A process shutdown puzzle
- Does creating a thread from DllMain deadlock or doesn't it?
Well it is waiting to get the module for the running (HMODULE) executable. You might want to see whether you have opened such a handle, especially when it is locked for shared reading.
Perhaps procexp.exe
from Sysinternals (Microsoft) could help finding the blocked handle. Or you can scrutinize any HMODULE handles, LoadLibrary calls to find the culprit
精彩评论