Native crash using DispatchQueue.Sumbit on iOS

I've been using GCD for a while now without issue but I'm now trying to use DispatchQueue.Submit (which as far as I can tell is the managed version of dispatch_apply) to process the callback concurrently. This causes a native crash in the mono runtime on both the simulator and a real device (iOS 12.1).

The code is

        CoreFoundation.DispatchQueue.DefaultGlobalQueue.Submit((i) => {
            Console.WriteLine($"Processing iteration {i}");
        }, 100);

Has anyone successfully used DispatchQueue.Submit on iOS? Is this another issue with Mono on iOS not being able to handle generics and callbacks from native to managed given Submit takes an Action? It could be partly to do with the signature for Submit isn't quite correct. It takes an Action but an int64 count. dispatch_apply takes a block taking a size_t and a count of size_t which will both be 64-bit.

The crash stack trace is:

2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 0 BlockScanner.iOS 0x0000000103f1bc80 mono_handle_native_crash + 224
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 1 BlockScanner.iOS 0x0000000103f280a0 mono_sigsegv_signal_handler + 316
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 2 libsystem_platform.dylib 0x0000000221260a20 + 56
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 3 BlockScanner.iOS 0x0000000103fb0750 mono_gchandle_is_in_domain + 20
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 4 BlockScanner.iOS 0x0000000103f5e400 mono_gc_GCHandle_CheckCurrentDomain + 32
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 5 BlockScanner.iOS 0x0000000102bed1d0 wrapper_managed_to_native_System_Runtime_InteropServices_GCHandle_CheckCurrentDomain_int + 96
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 6 BlockScanner.iOS 0x0000000102bed0f8 System_Runtime_InteropServices_GCHandle_op_Explicit_intptr + 56
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 7 BlockScanner.iOS 0x0000000102bed6bc System_Runtime_InteropServices_GCHandle_FromIntPtr_intptr + 28
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 8 BlockScanner.iOS 0x00000001038561fc CoreFoundation_DispatchQueue_static_dispatcher_iterations_to_managed_intptr_intptr + 44
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 9 BlockScanner.iOS 0x0000000103885a20 wrapper_native_to_managed_CoreFoundation_DispatchQueue_static_dispatcher_iterations_to_managed_intptr_intptr + 144
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 10 libdispatch.dylib 0x000000022108e4c4 + 16
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 11 libdispatch.dylib 0x00000002210403a4 + 164
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 12 libdispatch.dylib 0x000000022103fe34 + 720
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 13 BlockScanner.iOS 0x00000001038837e0 wrapper_managed_to_native_CoreFoundation_DispatchQueue_dispatch_apply_f_intptr_intptr_intptr_CoreFoundation_DispatchQueue_dispatch_callback_iterations_t + 128
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 14 BlockScanner.iOS 0x00000001038564cc CoreFoundation_DispatchQueue_Submit_System_Action_1_int_long + 124
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 15 BlockScanner.iOS 0x0000000102a70604 BlockScanner_App__ctor + 2052
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 16 BlockScanner.iOS 0x0000000102a71e0c BlockScanner_iOS_AppDelegate_FinishedLaunching_UIKit_UIApplication_Foundation_NSDictionary + 188
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 17 BlockScanner.iOS 0x0000000102d54388 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 248
2018-11-29 11:36:01.923 BlockScanner.iOS[7718:3061784] critical: 18 BlockScanner.iOS 0x0000000103f2b1c0 mono_jit_runtime_invoke + 904
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 19 BlockScanner.iOS 0x0000000103f98b0c mono_runtime_invoke_checked + 148
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 20 BlockScanner.iOS 0x0000000103f9c130 mono_runtime_invoke + 84
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 21 BlockScanner.iOS 0x0000000102a4e610 _ZL31native_to_managed_trampoline_10P11objc_objectP13objc_selectorPP11_MonoMethodP13UIApplicationP12NSDictionaryj + 688
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 22 BlockScanner.iOS 0x0000000102a4ec7c -[AppDelegate application:didFinishLaunchingWithOptions:] + 60
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 23 UIKitCore 0x000000024e2e80f4 + 412
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 24 UIKitCore 0x000000024e2e985c + 3340
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 25 UIKitCore 0x000000024e2ef2a8 + 1552
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 26 UIKitCore 0x000000024db8b358 + 784
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 27 UIKitCore 0x000000024db93fd8 + 160
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 28 UIKitCore 0x000000024db8afd4 + 240
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 29 UIKitCore 0x000000024db8b974 + 1076
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 30 UIKitCore 0x000000024db89a74 + 772
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 31 UIKitCore 0x000000024db89720 + 432
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 32 UIKitCore 0x000000024db8e8e0 + 220
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 33 UIKitCore 0x000000024db8f840 _performActionsWithDelayForTransitionContext + 112
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 34 UIKitCore 0x000000024db8e798 + 248
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 35 UIKitCore 0x000000024db93684 + 368
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 36 UIKitCore 0x000000024e2ed7a0 + 540
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 37 UIKitCore 0x000000024ded6bac + 364
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 38 FrontBoardServices 0x00000002240889d4 + 444
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 39 FrontBoardServices 0x000000022409379c + 260
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 40 FrontBoardServices 0x0000000224092e94 + 64
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 41 libdispatch.dylib 0x000000022108e484 + 16
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 42 libdispatch.dylib 0x0000000221031e20 + 224
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 43 FrontBoardServices 0x00000002240c7a9c + 40
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 44 FrontBoardServices 0x00000002240c7728 + 416
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 45 FrontBoardServices 0x00000002240c7d44 + 56
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 46 CoreFoundation 0x00000002215e41cc + 24
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 47 CoreFoundation 0x00000002215e414c + 88
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 48 CoreFoundation 0x00000002215e3a30 + 176
2018-11-29 11:36:01.924 BlockScanner.iOS[7718:3061784] critical: 49 CoreFoundation 0x00000002215de8fc + 1040
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 50 CoreFoundation 0x00000002215de1cc CFRunLoopRunSpecific + 436
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 51 GraphicsServices 0x0000000223855584 GSEventRunModal + 100
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 52 UIKitCore 0x000000024e2f1054 UIApplicationMain + 212
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 53 BlockScanner.iOS 0x0000000103882688 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 328
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 54 BlockScanner.iOS 0x00000001038149ac UIKit_UIApplication_Main_string___intptr_intptr + 44
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 55 BlockScanner.iOS 0x000000010381496c UIKit_UIApplication_Main_string___string_string + 172
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 56 BlockScanner.iOS 0x0000000102a71ca0 BlockScanner_iOS_Application_Main_string__ + 128
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 57 BlockScanner.iOS 0x0000000102d54388 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 248
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 58 BlockScanner.iOS 0x0000000103f2b1c0 mono_jit_runtime_invoke + 904
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 59 BlockScanner.iOS 0x0000000103f98b0c mono_runtime_invoke_checked + 148
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 60 BlockScanner.iOS 0x0000000103f9df70 mono_runtime_exec_main_checked + 120
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 61 BlockScanner.iOS 0x0000000103f11fc8 mono_jit_exec + 268
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 62 BlockScanner.iOS 0x000000010404844c xamarin_main + 2220
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 63 BlockScanner.iOS 0x0000000102a6fd7c main + 96
2018-11-29 11:36:01.925 BlockScanner.iOS[7718:3061784] critical: 64 libdyld.dylib 0x000000022109ebb4 + 4

Posts

  • HD4gGL9sHD4gGL9s Member ✭✭

    I've found the bug in CoreFoundation.Dispatch (xamarin-macios/src/CoreFoundation/Dispatch.cs). There are 2 issues:

    1) The handle allocated on line 411 is freed in the static callback line 336 but this means it is freed when the first iteration executes. It should be freed in Submit when the dispatch_apply_f call returns as at that point all iterations have finished and the handle is no longer required.
    2) The action passed in to DispatchQueue.Submit is an Action but line 335 tries to cast it to an Action

    For now I've got my own implementation to fix our issue but when I get a chance I'll submit a ticket with a patch.

Sign In or Register to comment.