Repository navigation
Description
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::ForceGCand 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::EnumThreadsand 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
tommcdon commentedon Oct 18, 2021
@davmason
ww898 commentedon Oct 19, 2021
I have just reproduced the issue on macOS Catalina/Big Sur x64. Looks like
InternalEndCurrentThreadWrapperwasn't called at the end ofICorProfilerInfo::ForceGCthread.davmason commentedon Oct 19, 2021
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?
ww898 commentedon Oct 20, 2021
Hi @davmason, Probably I found the root of the problems. It's using
pthread_setspecific/pthread_getspecificinside of the pthread key destroy callback.Register
ForceGCthread:Crash in
pthread_getspecificwhich prevent callDetachThread:The thread completely hangs on arm64 wihtout specific reason here:
janvorli commentedon Oct 20, 2021
@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.
davmason commentedon Oct 20, 2021
@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
janvorli commentedon Oct 21, 2021
@davmason a profiler repro would be nice
davmason commentedon Oct 22, 2021
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
janvorli commentedon Oct 22, 2021
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?
3 remaining items
janvorli commentedon Oct 23, 2021
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.
ww898 commentedon Oct 23, 2021
@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
ForceGCthread always throws the access violation or always freezed forever betweenInternalEndCurrentThreadWrapperandLOADCallDllMainSafeunder lldb. I really thought that freezes are because lldb has troubles on ARM64 platform because is new till got Catalina x64.janvorli commentedon Oct 23, 2021
@ww898 I am not seeing this behavior when running under LLDB on my BigSur x64 macOS, so I wonder what's different.
ww898 commentedon Oct 28, 2021
Hi @janvorli, I am going to try https://github.com/davmason/macos_thread_profiler_repro on my devices this week.
ww898 commentedon Nov 1, 2021
.NET 6.0 rc2 macOS Monterey arm64
I see
Saw ForceGC thread id in enum list!in logs:janvorli commentedon Nov 1, 2021
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?
ww898 commentedon Nov 1, 2021
I have just finished debugging .NET 5.0.11 macOS Big Sur x64 where the situation is the same. The
Thread::DetachThread()inEEDllMain()never called forForceGCthread because the condition in +193..+196:+399 is the address just after
Thread::DetachThread()GetThread()implementationI think that the problem is in following condition where the
GetThread()returnsnullptr: https://github.com/dotnet/runtime/blob/release/5.0/src/coreclr/src/vm/ceemain.cpp#L1864-L1865P.S. SIGSEGV was on macOS Catalina x64 only with our profiler.
P.P.S. May be the issue with
~TlsDestructionMonitorbecause__threadwas changed tothread_local? It's only the idea without confirmation.janvorli commentedon Nov 1, 2021
@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__threaddoesn'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.
ww898 commentedon Dec 1, 2021
I reproducted the issue (with some minor fixes in
build.shandrepro.sh) on Linux Ubuntu 20.04.3 LTS x64 on .NET v6.0.100 only (.NET 5.0 works properly):tommcdon commentedon May 17, 2022
@VSadov @davmason is this issue fixed?
davmason commentedon May 20, 2022
It was fixed in #65358