Skip to content

ProfileEnter crashes for UnmanagedCallersOnly methods invoked from foreign threads not registered with runtime yet. #115617

Description

@janvorli

Discussed in #115511

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 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?

Activity

  1. added this to the 10.0.0 milestone on May 15, 2025
  2. self-assigned this
    on May 15, 2025
  3. added theissue type on May 15, 2025
  4. dotnet-policy-service commented on May 15, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @tommcdon
    See info in area-owners.md if you want to be subscribed.

  5. janvorli commented on May 15, 2025

    @janvorli
    MemberAuthor

    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.

  6. added a commit that references this issue on Sep 3, 2025
    4f5e1a0
  7. added a commit that references this issue on Sep 4, 2025
    0147054
  8. added a commit that references this issue on Sep 4, 2025
    655ec8a
  9. added a commit that references this issue on Sep 11, 2025
    043fcd5
  10. locked and limited conversation to collaborators on Oct 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions