Repository navigation
Description
Description
For applications that host the runtime natively, inside a MacOS native application, when making a reverse interop call from native into managed, from within a background thread not created by the runtime, if that callback performs a GC, from time to time this will cause a crash.
In order to repro this, I provide here a link to a github repo. I tried to make it as small as possible: https://github.com/odhanson/M1Bug
Configuration
This happens in .NET 6 only, on MacOS. I confirmed it both on x64 and ARM64.
Regression?
This is a regression from .NET 5
Other information
A workaround is not to make managed callbacks from within the native thread pool. Instead, always make the callback from the main UI thread from which we bootstrapped the CLR, and then queue the work on a managed thread pool.
Callstack for the crash:
Thread 4 Crashed:: Dispatch queue: com.apple.root.default-qos
0 libsystem_kernel.dylib 0x0000000198f08e68 __pthread_kill + 8
1 libsystem_pthread.dylib 0x0000000198f3b43c pthread_kill + 292
2 libsystem_c.dylib 0x0000000198e83460 abort + 104
3 libcoreclr.dylib 0x0000000160838a34 PROCAbort + 60
4 libcoreclr.dylib 0x000000016080a354 InjectActivationInternal(CorUnix::CPalThread*) + 52
5 libcoreclr.dylib 0x000000016083e4f0 PAL_InjectActivation + 276
6 libcoreclr.dylib 0x0000000160a3cf58 ThreadSuspend::SuspendRuntime(ThreadSuspend::SUSPEND_REASON) + 1220
7 libcoreclr.dylib 0x0000000160a3eb90 ThreadSuspend::SuspendEE(ThreadSuspend::SUSPEND_REASON) + 288
8 libcoreclr.dylib 0x00000001609a7334 GCToEEInterface::SuspendEE(SUSPEND_REASON) + 48
9 libcoreclr.dylib 0x0000000160ac6140 WKS::GCHeap::GarbageCollectGeneration(unsigned int, gc_reason) + 420
10 libcoreclr.dylib 0x0000000160ac7644 WKS::gc_heap::trigger_gc_for_alloc(int, gc_reason, WKS::GCDebugSpinLock*, bool, WKS::msl_take_state) + 48
11 libcoreclr.dylib 0x0000000160ac82ec WKS::gc_heap::try_allocate_more_space(alloc_context*, unsigned long, unsigned int, int) + 780
12 libcoreclr.dylib 0x0000000160ae67b4 WKS::gc_heap::allocate_uoh_object(unsigned long, unsigned int, int, long&) + 100
13 libcoreclr.dylib 0x0000000160ae9da0 WKS::GCHeap::Alloc(gc_alloc_context*, unsigned long, unsigned int) + 68
14 libcoreclr.dylib 0x00000001609abf14 AllocateSzArray(MethodTable*, int, GC_ALLOC_FLAGS) + 288
15 libcoreclr.dylib 0x00000001609c97bc JIT_NewArr1(CORINFO_CLASS_STRUCT_*, long) + 152
16 ??? 0x0000000289a7b58c 0 + 10899404172
17 ??? 0x0000000289a7b3a8 0 + 10899403688
18 libMacWebDialogWindow.dylib 0x0000000165850a38 __62-[WebDialogWindowURLSchemeHandler webView:startURLSchemeTask:]_block_invoke + 108
19 libdispatch.dylib 0x0000000198d8a128 _dispatch_call_block_and_release + 32
20 libdispatch.dylib 0x0000000198d8bec0 _dispatch_client_callout + 20
21 libdispatch.dylib 0x0000000198d8f00c _dispatch_queue_override_invoke + 756
22 libdispatch.dylib 0x0000000198d9d684 _dispatch_root_queue_drain + 388
23 libdispatch.dylib 0x0000000198d9de90 _dispatch_worker_thread2 + 112
24 libsystem_pthread.dylib 0x0000000198f37860 _pthread_wqthread + 216
25 libsystem_pthread.dylib 0x0000000198f365d4 start_wqthread + 8
Activity
odhanson commentedon Aug 25, 2021
cc: @janvorli , @jkotas , @mangod9
janvorli commentedon Sep 7, 2021
Unfortunately, it turns out this is by design on macOS for work queue threads. They have
pthread_killexplicitly disabled. It is documented here: https://developer.apple.com/library/archive/documentation/General/Conceptual/ConcurrencyProgrammingGuide/ThreadMigration/ThreadMigration.html#//apple_ref/doc/uid/TP40008091-CH105-SW18It seems that we will need to ignore the failure to send the signal and rely on such threads to sync with GC, which they should eventually do.
jkotas commentedon Sep 7, 2021
The documentation says that
pthread_killcannot be called by the work queue thread. It does not say anything about the target thread. Is it ok for the target thread to be a work queue thread?The current (work queue) thread that is sending the signal is in a good spot already. I think the problem is with all other (non work queue) threads in the process. We won't be able to suspend them if they are running in infinite loop or long running compute loop. Maybe we can try to create a dummy thread that just does the
pthread_killwhen the direct call ofpthread_killfails?janvorli commentedon Sep 7, 2021
The doc is a bit vague and misleading then. I haven't read it that way. Anyways, the problem is not caused by the caller is of the
pthread_killbeing the work queue thread. The target thread is actually the one that's checked. See the implementation here:https://github.com/Apple-FOSS-Mirror/Libc/blob/master/pthreads/pthread.c#L1574-L1601