CoreCLR profiler check exists of native thread leading runtime crash? #115511
-
Greetings coreclr devs. This question is about CoreCLR profiler components and native thread that created outside coreclr runtime. Right now we are trying to add custom profiler to an embedded coreclr runtime, and it works as expected when under single thread mode, but meets some thread exits check failure under multi thread mode. The multi thread scenario is like: The producer create native thread that the coreclr runtime is not aware of, like async loading resources on native code, once load done, pass the loaded resource to the managed function callback that hooked before. That means we have a native thread invoking managed code, it works as expected too. Then we enable a coreclr profiler that implemented the ICorProfiler interface, and redo the process above, it triggers the runtime enter ProfileEnter, where there are thread valid check in proftoeeinterfaceimpl.cpp, like GCX_COOP_THREAD_EXISTS(GET_THREAD());
// where expanded with
inline Thread* GetThread()
{
Thread* pThread = gCurrentThreadInfo.m_pThread;
_ASSERTE(pThread); // pThread will be invalid as currently we are in the native thread that created outside
return pThread;
} and the assert will fail as the pThread is invalid value like 0xc0000005. By looking at the Thread info in visual studio, we confirm that current thread is a host's native thread. We would like to know if coreclr exposes api to register or attach the native thread to runtime before the ProfileEnter stage, to have the gCurrentThreadInfo contains necessary thread info representing the thread, other than invalid value? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
@janvorli Sorry for bothering you here, just saw you committed this thread check. The ThreadLocalInfo gCurrentThreadInfo has all it's member value 0 when failing the assert, seems this object is not assigned, is that by design with such procedural? Edit: runtime/src/coreclr/vm/proftoeeinterfaceimpl.cpp Line 10735 in 9bcd3e7 runtime/src/coreclr/vm/proftoeeinterfaceimpl.cpp Line 10729 in 9bcd3e7 |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan sorry for bothering you here too, could you please label this discussion and help me inform the relevant developer? |
Beta Was this translation helpful? Give feedback.
-
@AlexeiNaabal I've looked into it and found an old issue that discusses this problem: #68760. |
Beta Was this translation helpful? Give feedback.
Ah, you are right, the way it happens you don't have any control over it and my change from 2 years ago has actually caused this issue.
It seems that while I've added the GCX_COOP into the body after ensuring that the runtime has registered the thread, I've forgotten to remove the
GCX_COOP_THREAD_EXISTS(GET_THREAD());
from the header of the ProfileEnter.