You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ProfileEnter crashes for UnmanagedCallersOnly methods invoked from foreign threads not registered with runtime yet. #115617
Originally posted by AlexeiNaabal May 13, 2025
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:
We launched the embedded coreclr runtime with hostfxr api, then invoke some native function to feed native async task into a native task producer with managed function callback pointer for task consumer to invoke when task finished. When interacting with the task producer, we are under main thread so everything works fine.
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 withinline Thread* GetThread()
{
Thread* pThread = gCurrentThreadInfo.m_pThread;
_ASSERTE(pThread); // pThread will be invalid as currently we are in the native thread that created outsidereturn 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?
When I was fixing a ProfileEnter related issue two years ago, I have accidentally not removed an intermediate change that has added
GCX_COOP_THREAD_EXISTS(GET_THREAD()); to the header of the ProfileEnter. This causes the ProfileEnter to crash when the function that has this injected is marked with "UnmanagedCallersOnly" and called from a foreign thread that was not registered with the runtime yet, because the GET_THREAD() ends up returning NULL.
Activity
dotnet-policy-service commentedon May 15, 2025
Tagging subscribers to this area: @tommcdon
See info in area-owners.md if you want to be subscribed.
janvorli commentedon May 15, 2025
When I was fixing a ProfileEnter related issue two years ago, I have accidentally not removed an intermediate change that has added
GCX_COOP_THREAD_EXISTS(GET_THREAD()); to the header of the ProfileEnter. This causes the ProfileEnter to crash when the function that has this injected is marked with "UnmanagedCallersOnly" and called from a foreign thread that was not registered with the runtime yet, because the GET_THREAD() ends up returning NULL.
Fix ProfileEnter when called on a foreign thread
Fix ProfileEnter when called on a foreign thread
Fix ProfileEnter when called on a foreign thread (dotnet#119327)
[release/10.0] Fix ProfileEnter when called on a foreign thread (#119349