Skip to content

The dead thread reported in ICorProfilerInfo4::EnumThreads #60566

Closed

Description

@ww898

Description

As described in documentation ICorProfilerInfo::ForceGC requires separate native thread for working. The thread will be finished by my profiler during detaching procedure. And I see it in lldb. However, after attaching again to the process the ICorProfilerInfo4::EnumThreads call returns me the already dead thread where ICorProfilerInfo::ForceGC was called sometime ago .

It's the important problem because the thread id can be already reused for a new native thread.

My investigation shows the CoreCLR keeps the static list of managed threads. and only alive threads will be returned in ICorProfilerInfo4::EnumThreads. On the other hand the ICorProfilerInfo::ForceGC adds current thread in this list.

Reproduction Steps

  • Create following "hello world" application:
using System;
namespace test { class Program { static void Main(string[] args) {
  Console.WriteLine("Hello World!");
  Console.ReadLine();
}}}
  • Publish it as a self contained application and run it in separate console
  • Attach and detach a profiler which use ICorProfilerInfo::ForceGC and dump somewhere the thread id after the call.
  • Attach lldb and dump the thread list. Check that the thread id is not in the list.
  • Attach a profiler which use ICorProfilerInfo4::EnumThreads and see that the dead thread id is in the list of managed threads.

Actual behavior

The native thread id which interesting us is 1BCF69.

The part of the first profiler log;

00000000001BCF69 0 0000000000000000 GCCQ::ForceGcThread
00000000001BCF69 0 0000000000000000 GCCQ::ForceGc // <-- ICorProfilerInfo::ForceGC started
00000000001BCF69 1 000000014180AA00 MProf::GarbageCollectionStarted gcNumber=0 reason=induced c=5
00000000001BCF69 1 000000014180AA00 GCCQ::OnStarted gcNumber=0 gcState=normal
00000000001BCF69 1 000000014180AA00 MDG2::FlushMetadata
00000000001BCF69 1 000000014180AA00 MDG2::FlushMetadata res=ok
00000000001BCF69 1 000000014180AA00 MProf::GarbageCollectionStarted gcNumber=0 res=ok
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122F650
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FA50
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FC40
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FCC0
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FD40
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FDC0
00000000001BCF69 1 000000014180AA00 MProf::FinalizeableObjectQueued f=C oid=000000010122FE48
00000000001BCF69 1 000000014180AA00 MProf::SurvivingReferences2 c=1
00000000001BCF69 1 000000014180AA00 MProf::SurvivingReferences2 c=91
00000000001BCF69 1 000000014180AA00 MProf::RootReferences2 c=42
00000000001BCF69 1 000000014180AA00 BProf::RootReferences c=42
00000000001BCF69 1 000000014180AA00 MProf::ObjectReferences oid=0000000101220048 cid=0000000280E48C80 c=0
00000000001BCF69 1 000000014180AA00 MProf::GarbageCollectionFinished gcNumber=0
00000000001BCF69 1 000000014180AA00 MProf::GarbageCollectionFinished gcNumber=0 res=ok
00000000001BCF69 1 000000014180AA00 GCCQ::OnFinished gcNumber=0 gcState=normal
00000000001BCF69 0 0000000000000000 GCCQ::ForceGc res=ok  // <-- ICorProfilerInfo::ForceGC finished
00000000001BCF69 0 0000000000000000 GCCQ::ForceGcThread res=ok

lldb check:

(lldb) thread list
Process 52251 stopped
* thread #1: tid = 0x1bce01, 0x0000000194e1a538 libsystem_kernel.dylib`read + 8, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  thread #2: tid = 0x1bce03, 0x0000000194e19954 libsystem_kernel.dylib`mach_msg_trap + 8
  thread #3: tid = 0x1bce04, 0x0000000194e1f8cc libsystem_kernel.dylib`kevent + 8
  thread #4: tid = 0x1bce05, 0x0000000194e21ad0 libsystem_kernel.dylib`poll + 8
  thread #5: tid = 0x1bce06, 0x0000000194e1a3d4 libsystem_kernel.dylib`__open + 8
  thread #6: tid = 0x1bce07, 0x0000000194e1d0c0 libsystem_kernel.dylib`__psynch_cvwait + 8
  thread #7: tid = 0x1bce08, 0x0000000194e1d0c0 libsystem_kernel.dylib`__psynch_cvwait + 8
  thread #8: tid = 0x1bce0a, 0x0000000194e1a538 libsystem_kernel.dylib`read + 8
  thread #9: tid = 0x1bcf5c, 0x0000000194e1d0c0 libsystem_kernel.dylib`__psynch_cvwait + 8
  thread #10: tid = 0x1bcf5d, 0x0000000194e1b604 libsystem_kernel.dylib`__workq_kernreturn + 8

The part of the second profiler log:

00000000001BCE05 1 0000000000000000 BProf::ProfilerAttachComplete // <--- ICorProfilerCallback3::ProfilerAttachComplete
00000000001BCE05 1 0000000000000000 BProf::ProfilerAttachComplete tid=000000015200C000
00000000001BCE05 1 0000000000000000 BPPool::DoThreadRecovered tid=000000015200C000 osTid=001BCE01
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000015200C000 osTid=001BCE01 (1822209)
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000015200C000 id=00000001
00000000001BCE05 1 0000000000000000 BProf::ProfilerAttachComplete tid=000000015200C600
00000000001BCE05 1 0000000000000000 BPPool::DoThreadRecovered tid=000000015200C600 osTid=001BCE08
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000015200C600 osTid=001BCE08 (1822216)
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000015200C600 id=00000002
00000000001BCE05 1 0000000000000000 BProf::ProfilerAttachComplete tid=0000000131808200
00000000001BCE05 1 0000000000000000 BPPool::DoThreadRecovered tid=0000000131808200 osTid=001BCE0A
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=0000000131808200 osTid=001BCE0A (1822218)
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=0000000131808200 id=00000003
00000000001BCE05 1 0000000000000000 BProf::ProfilerAttachComplete tid=000000014180AA00 // <--- The dead thread reported here!!!
00000000001BCE05 1 0000000000000000 BPPool::DoThreadRecovered tid=000000014180AA00 osTid=001BCF69
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000014180AA00 osTid=001BCF69 (1822569)
00000000001BCE05 1 0000000000000000 BPool::DoThreadRecovered tid=000000014180AA00 id=00000004
00000000001BCE05 1 0000000000000000 ERROR: No such thread

Configuration

I reproduced the issue on macOS:

  • Catalina x64: .NET v5.0.402 x64
  • Big Sur x64: .NET v5.0.402 x64 / v6.0-rc2 x64
  • Monterey arm64: .NET v5.0.402 x64 + Rosetta2 / v6.0-rc2 x64 + Rosetta2 / v6.0-rc2 arm64

The Linux x64/arm64 versions work properly.

Activity

  1. ghost added
    untriaged
    on Oct 18, 2021
  2. added this to the 7.0.0 milestone on Oct 18, 2021
  3. removed
    untriaged
    on Oct 18, 2021
  4. tommcdon commented on Oct 18, 2021

    @tommcdon
    Member
  5. ww898 commented on Oct 19, 2021

    @ww898
    ContributorAuthor

    I have just reproduced the issue on macOS Catalina/Big Sur x64. Looks like InternalEndCurrentThreadWrapper wasn't called at the end of ICorProfilerInfo::ForceGC thread.

  6. davmason commented on Oct 19, 2021

    @davmason
    Contributor

    Hi @ww898, thanks for reporting this. Would you be able to create a simplified repro of a profiler that just creates the GC thread and then iterates the threads?

  7. ww898 commented on Oct 20, 2021

    @ww898
    ContributorAuthor

    Hi @davmason, Probably I found the root of the problems. It's using pthread_setspecific / pthread_getspecific inside of the pthread key destroy callback.

    Register ForceGC thread:

    * thread #11, stop reason = breakpoint 1.1
        frame #0: 0x0000000100a0650d libcoreclr.dylib`ThreadStore::AddThread(newThread=0x00007fabe1009600, bRequiresTSL=YES) at threads.cpp:5234:9 [opt]
    Target 0: (test) stopped.
    (lldb) bt
    * thread #11, stop reason = breakpoint 1.1
      * frame #0: 0x0000000100a0650d libcoreclr.dylib`ThreadStore::AddThread(newThread=0x00007fabe1009600, bRequiresTSL=YES) at threads.cpp:5234:9 [opt]
        frame #1: 0x0000000100a05647 libcoreclr.dylib`SetupThread(fInternal=<unavailable>) at threads.cpp:791:5 [opt]
        frame #2: 0x0000000100a0519a libcoreclr.dylib`SetupThreadNoThrow(int*) [inlined] SetupThread() at threads.h:600:12 [opt]
        frame #3: 0x0000000100a05193 libcoreclr.dylib`SetupThreadNoThrow(pHR=0x0000700003ec9db8) at threads.cpp:615 [opt]
        frame #4: 0x0000000100b2f572 libcoreclr.dylib`ETW::GCLog::ForceGCForDiagnostics() at eventtrace.cpp:953:9 [opt]
        frame #5: 0x0000000100aebb32 libcoreclr.dylib`ProfToEEInterfaceImpl::ForceGC(this=<unavailable>) at proftoeeinterfaceimpl.cpp:4641:18 [opt]
        frame #6: 0x00000001033000a6 libJetBrains.Profiler.Core.dylib`jbprof::gc_command_queue::thread_data::force_gc_thread(jb::unique_hld<void*, jb::delete_module_handle, std::__1::integral_constant<void*, (void*)0> >&) + 438
        frame #7: 0x00000001032fe78f libJetBrains.Profiler.Core.dylib`jbprof::gc_command_queue::raw_force_gc_thread(void*) + 143
        frame #8: 0x00007fff6e37c109 libsystem_pthread.dylib`_pthread_start + 148
        frame #9: 0x00007fff6e377b8b libsystem_pthread.dylib`thread_start + 15```
    

    Crash in pthread_getspecific which prevent call DetachThread:

    Process 9960 stopped
    * thread #11, stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
        frame #0: 0x00007fff6e377bf5 libsystem_pthread.dylib`pthread_getspecific
    libsystem_pthread.dylib`pthread_getspecific:
    ->  0x7fff6e377bf5 <+0>: movq   %gs:(,%rdi,8), %rax
        0x7fff6e377bfe <+9>: retq   
    
    libsystem_pthread.dylib`pthread_setspecific:
        0x7fff6e377bff <+0>: leaq   -0xa(%rdi), %rcx
        0x7fff6e377c03 <+4>: movl   $0x16, %eax
      thread #22, stop reason = breakpoint 4.1
        frame #0: 0x00000001008d45cd libcoreclr.dylib`LOADCallDllMainSafe(module=0x00007fabde60d230, dwReason=2, lpReserved=0x0000000000000000) at module.cpp:1250:18 [opt]
    Target 0: (test) stopped.
    (lldb) bt
    * thread #11, stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
      * frame #0: 0x00007fff6e377bf5 libsystem_pthread.dylib`pthread_getspecific
        frame #1: 0x00000001008d4454 libcoreclr.dylib`::LOADCallDllMain(DWORD, LPVOID) [inlined] CorUnix::GetCurrentPalThread() at thread.hpp:684:46 [opt]
        frame #2: 0x00000001008d4444 libcoreclr.dylib`::LOADCallDllMain(DWORD, LPVOID) [inlined] CorUnix::InternalGetCurrentThread() at thread.hpp:689 [opt]
        frame #3: 0x00000001008d4444 libcoreclr.dylib`::LOADCallDllMain(dwReason=3, lpReserved=0x0000000000000000) at module.cpp:1068 [opt]
        frame #4: 0x0000000100900738 libcoreclr.dylib`InternalEndCurrentThreadWrapper(arg=0x00007fabe100ca00) at thread.cpp:167:5 [opt]
        frame #5: 0x00007fff6e37a009 libsystem_pthread.dylib`_pthread_tsd_cleanup + 476
        frame #6: 0x00007fff6e37c512 libsystem_pthread.dylib`_pthread_exit + 70
        frame #7: 0x00007fff6e379e08 libsystem_pthread.dylib`pthread_exit + 42
    

    The thread completely hangs on arm64 wihtout specific reason here:

    (lldb) bt                                                                                                             
    * thread #20                                                                                                          
      * frame #0: 0x00000001034d6908 libcoreclr.dylib`::LOADCallDllMain(DWORD, LPVOID) [inlined] CorUnix::GetCurrentPalThread() at thread.hpp:684:66 [opt]                                                                                      
        frame #1: 0x00000001034d6908 libcoreclr.dylib`::LOADCallDllMain(DWORD, LPVOID) [inlined] CorUnix::InternalGetCurrentThread() at thread.hpp:689:31 [opt]                                                                                 
        frame #2: 0x00000001034d6908 libcoreclr.dylib`::LOADCallDllMain(dwReason=3, lpReserved=0x0000000000000000) at module.cpp:1076:15 [opt]                                                                                                  
        frame #3: 0x00000001034fd394 libcoreclr.dylib`InternalEndCurrentThreadWrapper(arg=0x000000012580a600) at thread.cpp:172:5 [opt]                                                                                                         
        frame #4: 0x00000001aea0ee14 libsystem_pthread.dylib`_pthread_tsd_cleanup + 448                                   
        frame #5: 0x00000001aea11be0 libsystem_pthread.dylib`_pthread_exit + 84
        frame #6: 0x00000001aea0ebe8 libsystem_pthread.dylib`pthread_exit + 88
        frame #7: 0x00000001aea0eb90 libsystem_pthread.dylib`_pthread_wqthread_exit + 80
    
  8. janvorli commented on Oct 20, 2021

    @janvorli
    Member

    @ww898 thank you a lot for the details! I'll take a look into it. We do something that may be questionable in the InternalEndCurrentThreadWrapper given the fact that it is called from pthread_exit. We call the pthread_setspecific and that might be causing things to break.

  9. davmason commented on Oct 20, 2021

    @davmason
    Contributor

    @janvorli let me know if this is enough information for you to investigate. I'm happy to help set up a profiler repro if you would like

  10. janvorli commented on Oct 21, 2021

    @janvorli
    Member

    @davmason a profiler repro would be nice

  11. davmason commented on Oct 22, 2021

    @davmason
    Contributor

    Here's a repro: https://github.com/davmason/macos_thread_profiler_repro

    All you should have to do is update the path to a coreclr repo here, then ./build.sh and ./repro.sh

  12. janvorli commented on Oct 22, 2021

    @janvorli
    Member

    I have tried that locally. While I can see the message "Saw ForceGC thread id in enum list!" printed out, I never got a crash in the LOADCallDllMain like shown above. And looking at how the profiler thread enumeration works, it can easily happen that it returns a thread that doesn't exist anymore. The enumerating loop in ProfilerThreadEnum::Init is called under the thread store lock, but after it returns, threads can exit. Am I missing something?

  13. 3 remaining items

  14. janvorli commented on Oct 23, 2021

    @janvorli
    Member

    But as for the DetachThread, I've just put a breakpoint on it and we never reach that function. In fact, even the TlsDestructionMonitor::~TlsDestructionMonitor is never called. That's a way we detect thread destruction. We have a thread_local variable of that type and expect the destructor to be called when a thread is going away. That is pretty strange, I am looking into it.

  15. ww898 commented on Oct 23, 2021

    @ww898
    ContributorAuthor

    @davmason Hi David,
    it looks like macOS has the special behavior for thread in the pthread key destroy handler. I think so because the application never thrown an unhandled exception without a debugger. However, the ForceGC thread always throws the access violation or always freezed forever between InternalEndCurrentThreadWrapper and LOADCallDllMainSafe under lldb. I really thought that freezes are because lldb has troubles on ARM64 platform because is new till got Catalina x64.

  16. janvorli commented on Oct 23, 2021

    @janvorli
    Member

    @ww898 I am not seeing this behavior when running under LLDB on my BigSur x64 macOS, so I wonder what's different.

  17. ww898 commented on Oct 28, 2021

    @ww898
    ContributorAuthor
  18. ww898 commented on Nov 1, 2021

    @ww898
    ContributorAuthor

    .NET 6.0 rc2 macOS Monterey arm64

    I see Saw ForceGC thread id in enum list! in logs:

    % ./repro.sh
    Warning NETSDK1174: The abbreviation of -p for --project is deprecated. Please use --project.
    Building...
    Initialize profiler!
    Launching ForceGC thread
    Thread created, id=48838a00
    Thread created, id=4902b800
    Thread created, id=4902da00
    Trying to force gc
    Thread created, id=38808200
    GC forced
    ForceGC threadId=38808200
    Thread created, id=3900a200
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread created, id=3900ae00
    Thread created, id=4889b800
    Thread created, id=39014a00
    Thread created, id=48892a00
    Thread created, id=48897600
    Thread created, id=3901e200
    Thread created, id=4904b000
    Thread created, id=4904be00
    Thread created, id=488a5e00
    Thread created, id=4904cc00
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    warn: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[5]
          The application is trying to access the ASP.NET Core developer certificate key. A prompt might appear to ask for permission to access the key. When that happens, select 'Always Allow' to grant 'dotnet' access to the certificate key in the future.
    fail: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[4]
          The ASP.NET Core developer certificate is in an invalid state. To fix this issue, run the following commands 'dotnet dev-certs https --clean' and 'dotnet dev-certs https' to remove all existing ASP.NET Core development certificates and create a new untrusted developer certificate. On macOS or Windows, use 'dotnet dev-certs https --trust' to trust the new certificate.
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: https://localhost:5001
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down.
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Development
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: /Users/jetbrains/work/PROF-1182/macos_thread_profiler_repro/webapp
    Thread destroyed, id=4902da00
    Thread created, id=39808200
    Thread destroyed, id=4904cc00
    Thread destroyed, id=488a5e00
    Thread destroyed, id=4889b800
    Thread destroyed, id=48892a00
    Thread destroyed, id=4904be00
    Thread destroyed, id=4904b000
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=39808200
    Thread created, id=39808200
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=39808200
    ^CThread created, id=18808200
    ^CCalling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread created, id=18809600
    Thread created, id=1880aa00
    Thread created, id=39808200
    Thread created, id=39809a00
    Thread created, id=29008c00
    info: Microsoft.Hosting.Lifetime[0]
          Application is shutting down...
    Thread destroyed, id=39808200
    Thread destroyed, id=1880aa00
    Thread created, id=39809000
    Thread created, id=488a0200
    Thread created, id=4904aa00
    Thread created, id=3902ec00
    Thread created, id=3880ae00
    Thread created, id=3880bc00
    Thread created, id=49048c00
    Thread destroyed, id=3900ae00
    
  19. janvorli commented on Nov 1, 2021

    @janvorli
    Member

    Right, that's what I see too. The reason for those is what I've mentioned above in #60566 (comment) . But I got no SIGSEGVs when running under a debugger. Do you still get those like with your profiler?

  20. ww898 commented on Nov 1, 2021

    @ww898
    ContributorAuthor

    I have just finished debugging .NET 5.0.11 macOS Big Sur x64 where the situation is the same. The Thread::DetachThread() in EEDllMain() never called for ForceGC thread because the condition in +193..+196:

        0x10246a591 <+97>:  callq  0x10200afe0               ; PAL_GetNativeExceptionHolderHead
        0x10246a596 <+102>: movq   %rax, -0x38(%rbp)
        0x10246a59a <+106>: movq   (%rax), %rcx
        0x10246a59d <+109>: movq   %rcx, -0x30(%rbp)
        0x10246a5a1 <+113>: leaq   -0x40(%rbp), %rcx
        0x10246a5a5 <+117>: movq   %rcx, (%rax)
        0x10246a5a8 <+120>: movq   -0x48(%rbp), %rax
        0x10246a5ac <+124>: movl   0x8(%rax), %eax
        0x10246a5af <+127>: testl  %eax, %eax
        0x10246a5b1 <+129>: je     0x10246a618               ; <+232>
        0x10246a5b3 <+131>: cmpl   $0x3, %eax
        0x10246a5b6 <+134>: je     0x10246a5e5               ; <+181>
        0x10246a5b8 <+136>: cmpl   $0x1, %eax
        0x10246a5bb <+139>: jne    0x10246a6bf               ; <+399>
        0x10246a5c1 <+145>: leaq   0xd3fd0(%rip), %rdi       ; g_SystemInfo
        0x10246a5c8 <+152>: callq  0x102023fc0               ; GetSystemInfo
        0x10246a5cd <+157>: leaq   0x4cc(%rip), %rdi         ; GetThreadUICultureNames(StringArrayList*)
        0x10246a5d4 <+164>: leaq   0xb05(%rip), %rsi         ; GetThreadUICultureId(char16_t (*) [85])
        0x10246a5db <+171>: callq  0x10233c400               ; SetResourceCultureCallbacks(int (*)(StringArrayList*), int (*)(char16_t (*) [85]))
        0x10246a5e0 <+176>: jmp    0x10246a6bf               ; <+399>
        0x10246a5e5 <+181>: leaq   0xcbb84(%rip), %rdi       ; gCurrentThreadInfo
        0x10246a5ec <+188>: callq  *(%rdi)
        0x10246a5ee <+190>: movq   (%rax), %rbx
        0x10246a5f1 <+193>: testq  %rbx, %rbx
    ->  0x10246a5f4 <+196>: je     0x10246a6bf               ; <+399>
    

    +399 is the address just after Thread::DetachThread()

        0x10246a6aa <+378>: movq   %rbx, %rdi
        0x10246a6ad <+381>: callq  0x1022683c0               ; Thread::RareEnablePreemptiveGC()
        0x10246a6b2 <+386>: movq   %rbx, %rdi
        0x10246a6b5 <+389>: movl   $0x1, %esi
        0x10246a6ba <+394>: callq  0x1021493a0               ; Thread::DetachThread(int)
        0x10246a6bf <+399>: leaq   0xaa84a(%rip), %rax       ; vtable for NativeExceptionHolderBase + 16
        0x10246a6c6 <+406>: movq   %rax, -0x40(%rbp)
        0x10246a6ca <+410>: movq   -0x38(%rbp), %rax
    

    GetThread() implementation

    inline Thread* GetThread()
    {
        Thread* pThread = gCurrentThreadInfo.m_pThread;
        _ASSERTE(pThread);
        return pThread;
    }
    

    I think that the problem is in following condition where the GetThread() returns nullptr: https://github.com/dotnet/runtime/blob/release/5.0/src/coreclr/src/vm/ceemain.cpp#L1864-L1865

    P.S. SIGSEGV was on macOS Catalina x64 only with our profiler.
    P.P.S. May be the issue with ~TlsDestructionMonitor because __thread was changed to thread_local? It's only the idea without confirmation.

  21. janvorli commented on Nov 1, 2021

    @janvorli
    Member

    @ww898 thank you for all the details. The issue was caused by a bit different change. We were relying on DllMain being called on thread attach / detach before and then switched to the thread_local. The __thread doesn't support any constructors / destructors on objects at all.
    So it seems the SIGSEGV is somehow related to an interaction between your profiler and our PAL. I wonder if you also have a thread exit handler registered that would end up being called before our one and do something that our one doesn't like.

  22. ww898 commented on Dec 1, 2021

    @ww898
    ContributorAuthor

    I reproducted the issue (with some minor fixes in build.sh and repro.sh) on Linux Ubuntu 20.04.3 LTS x64 on .NET v6.0.100 only (.NET 5.0 works properly):

    ww898@ww898u-x64:~/work/macos_thread_profiler_repro$ ./build.sh
      CORECLR_PATH : /home/ww898/work/runtime/src/coreclr
      CORECLR_BIN  : /home/ww898/work/runtime/artifacts/bin/coreclr/Ubuntu.x64.Debug
      BuildOS      : Ubuntu
      BuildArch    : x64
      BuildType    : Debug
      Building ...
    ~/work/macos_thread_profiler_repro/bin ~/work/macos_thread_profiler_repro
    -- The C compiler identification is Clang 10.0.0
    -- The CXX compiler identification is Clang 10.0.0
    -- Check for working C compiler: /usr/bin/clang
    -- Check for working C compiler: /usr/bin/clang -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/clang++
    -- Check for working CXX compiler: /usr/bin/clang++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    Printing env vars
    /home/ww898/work/runtime/src/coreclr/pal/inc/rt
    /home/ww898/work/runtime/src/coreclr/pal/inc
    /home/ww898/work/runtime/src/coreclr/inc
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/ww898/work/macos_thread_profiler_repro/bin
    Scanning dependencies of target CorProfiler
    [ 16%] Building CXX object CMakeFiles/CorProfiler.dir/src/common.cpp.o
    [ 33%] Building CXX object CMakeFiles/CorProfiler.dir/src/dllmain.cpp.o
    [ 66%] Building CXX object CMakeFiles/CorProfiler.dir/src/ClassFactory.cpp.o
    [ 66%] Building CXX object CMakeFiles/CorProfiler.dir/home/ww898/work/runtime/src/coreclr/pal/prebuilt/idl/corprof_i.cpp.o
    [ 83%] Building CXX object CMakeFiles/CorProfiler.dir/src/CorProfiler.cpp.o
    /home/ww898/work/macos_thread_profiler_repro/src/CorProfiler.cpp:72:57: warning: format specifies type 'unsigned int' but the argument has type 'ThreadID' (aka 'unsigned long') [-Wformat]
                            printf("ForceGC threadId=%x\n", forceGCThreadID);
                                                     ~~     ^~~~~~~~~~~~~~~
                                                     %lx
    /home/ww898/work/macos_thread_profiler_repro/src/CorProfiler.cpp:215:39: warning: format specifies type 'unsigned int' but the argument has type 'ThreadID' (aka 'unsigned long') [-Wformat]
        printf("Thread created, id=%x\n", threadId);
                                   ~~     ^~~~~~~~
                                   %lx
    /home/ww898/work/macos_thread_profiler_repro/src/CorProfiler.cpp:260:41: warning: format specifies type 'unsigned int' but the argument has type 'ThreadID' (aka 'unsigned long') [-Wformat]
        printf("Thread destroyed, id=%x\n", threadId);
                                     ~~     ^~~~~~~~
                                     %lx
    3 warnings generated.
    [100%] Linking CXX shared library libCorProfiler.so
    [100%] Built target CorProfiler
    ~/work/macos_thread_profiler_repro
      Copying libCorProfiler.so to main directory
    Done.
    ww898@ww898u-x64:~/work/macos_thread_profiler_repro$ ./repro.sh
    /home/ww898/work/macos_thread_profiler_repro/libCorProfiler.so
    Initialize profiler!
    Launching ForceGC thread
    Thread created, id=a5762ec0
    Thread created, id=a57908f0
    Thread created, id=a57aeb50
    Thread created, id=a58c1b90
    Thread created, id=a58c31a0
    Thread created, id=ec001600
    Thread created, id=e4001a40
    Thread created, id=ec0068c0
    Thread created, id=ec009620
    Thread created, id=ec00ae20
    Thread created, id=ec01aa50
    Thread created, id=cc000c60
    Warning NETSDK1174: The abbreviation of -p for --project is deprecated. Please use --project.
    Building...
    Thread created, id=ec035dc0
    Trying to force gc
    Thread created, id=b8000c60
    GC forced
    ForceGC threadId=b8000c60
    Thread created, id=e4058bd0
    Thread created, id=ec03a280
    Thread created, id=ec03be20
    Thread created, id=e405a240
    Thread created, id=e4026c00
    Thread created, id=ec03de90
    Thread created, id=b0262850
    Thread created, id=ec0402d0
    Thread created, id=b02d8bd0
    Initialize profiler!
    Launching ForceGC thread
    Thread created, id=be2b7db0
    Thread created, id=be30c730
    Thread created, id=be32b150
    Thread created, id=be44e4e0
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Trying to force gc
    Thread created, id=4c000c60
    GC forced
    ForceGC threadId=4c000c60
    Thread created, id=be41a700
    Thread created, id=be517a30
    Thread created, id=be518540
    Thread created, id=44001ea0
    Thread created, id=be4d7200
    Thread created, id=be5c2e10
    Thread created, id=3c001860
    Thread created, id=be677bf0
    Thread created, id=380016a0
    Thread created, id=be5c5460
    Thread created, id=2c0018d0
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: https://localhost:5001
    info: Microsoft.Hosting.Lifetime[14]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down.
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Development
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: /home/ww898/work/macos_thread_profiler_repro/webapp
    Thread created, id=d8012bd0
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=be32b150
    Thread created, id=40000cb0
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=a57aeb50
    ^CThread created, id=50000c60
    Thread created, id=c0000c60
    Calling EnumThreads because we have observed the GC thread being destroyed
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Saw ForceGC thread id in enum list!
    Thread created, id=c00025c0
    Thread created, id=500027f0
    Calling EnumThreads because we have observed the GC thread being destroyed
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=500027f0
    Thread destroyed, id=c00025c0
    info: Microsoft.Hosting.Lifetime[0]
          Application is shutting down...
    Thread created, id=e80e2ff0
    Thread created, id=20001180
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Calling EnumThreads because we have observed the GC thread being destroyed
    Now enumerating threads
    Saw ForceGC thread id in enum list!
    Thread destroyed, id=be41a700
    ww898@ww898u-x64:~/work/macos_thread_profiler_repro$
    
  23. tommcdon commented on May 17, 2022

    @tommcdon
    Member

    @VSadov @davmason is this issue fixed?

  24. self-assigned this
    on May 17, 2022
  25. davmason commented on May 20, 2022

    @davmason
    Contributor

    It was fixed in #65358

  26. ghost locked as resolved and limited conversation to collaborators on Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions