xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.45k stars 511 forks source link

BGTaskScheduler crash #7456

Closed FlxGen closed 4 years ago

FlxGen commented 4 years ago

***PLEASE NOTE: This crash only occurs AFTER submitting app to the App Store, it does not happen when running the app locally. It may be an iOS bug but I'm opening it here because of the System.Reflection line in the stack trace. I'm not using any reflection in this part of my code (I've stripped out all my code in these blocks) so it's curious that after the background task is run that reflection is the next line. The iOS Build options are exactly the same between debug and release. Xcode version 11.2.1 (11B500).

Steps to Reproduce

  1. BGTaskScheduler.Shared.Register("com.ios.dev.syncdata", null, task => { }
  2. var appRefreshTaskRequest = new BGAppRefreshTaskRequest("com.ios.dev.syncdata") { EarliestBeginDate = null }; BGTaskScheduler.Shared.Submit(appRefreshTaskRequest, out NSError error);
  3. Lock device.
  4. Crashes when BGTask is run.

Expected Behavior

Execute BGTask without crash.

Actual Behavior

Incident Identifier: 3D307E12-D51B-4DCF-97DA-D12F4046D739
Beta Identifier:     DD1CF861-8AF7-4F88-93C2-BF099BF89037
Hardware Model:      iPhone12,3
Process:             App [15869]
Path:                /private/var/containers/Bundle/Application/346A742B-C439-4F35-B5B6-675EBA503973/App.app/App
Identifier:          com..ios.dev
Version:             1.89 (1.89)
AppStoreTools:       11B48b
AppVariant:          1:iPhone12,3:13
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                Non UI
Parent Process:      launchd [1]
Coalition:           com.app.ios.dev [1677]

Date/Time:           2019-11-15 08:38:18.9818 -0600
Launch Time:         2019-11-15 08:29:15.8363 -0600
OS Version:          iPhone OS 13.2.2 (17B102)
Release Type:        User
Baseband Version:    1.02.18
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  1

Thread 1 name:
Thread 1 Crashed:
0   libsystem_kernel.dylib          0x000000018ca2aefc __pthread_kill + 8
1   libsystem_pthread.dylib         0x000000018c94ad10 pthread_kill + 196 (pthread.c:1456)
2   libsystem_c.dylib               0x000000018c8daaf4 __abort + 116 (abort.c:147)
3   libsystem_c.dylib               0x000000018c8daa80 abort + 116 (abort.c:118)
4   App                             0x00000001056c49dc xamarin_unhandled_exception_handler.cold.1 + 36
5   App                             0x00000001056bcb3c xamarin_unhandled_exception_handler + 12 (runtime.m:1176)
6   App                             0x00000001055ceee8 mono_invoke_unhandled_exception_hook + 132
7   App                             0x0000000105570a0c mono_handle_exception_internal + 5580
8   App                             0x000000010556f434 mono_handle_exception + 56
9   App                             0x0000000105566588 mono_arm_throw_exception + 376
10  App                             0x000000010281067c throw_exception + 172
11  App                             0x0000000105624a7c mono_raise_exception + 64
12  App                             0x00000001056bc954 xamarin_process_managed_exception + 276 (runtime.m:2441)
13  App                             0x0000000105570770 mono_handle_exception_internal + 4912
14  App                             0x000000010556f434 mono_handle_exception + 56
15  App                             0x0000000105566588 mono_arm_throw_exception + 376
16  App                             0x000000010281067c throw_exception + 172
17  App                             0x000000010299c8d0 mscorlib_System_Reflection_RuntimeConstructorInfo_DoInvoke_object_System_Reflection_BindingFlags_... + 404 (RuntimeMethodInfo.cs:0)
18  BackgroundTasks                 0x00000001b5ccded0 __41-[BGTaskScheduler _runTask:registration:]_block_invoke.206 + 224 (BGTaskScheduler.m:446)
19  libdispatch.dylib               0x000000018c8e4b7c _dispatch_call_block_and_release + 32 (init.c:1408)
20  libdispatch.dylib               0x000000018c8e5fd8 _dispatch_client_callout + 20 (object.m:495)
21  libdispatch.dylib               0x000000018c8e8414 _dispatch_queue_override_invoke + 672 (inline_internal.h:2484)
22  libdispatch.dylib               0x000000018c8f4bd4 _dispatch_root_queue_drain + 348 (inline_internal.h:2525)
23  libdispatch.dylib               0x000000018c8f5384 _dispatch_worker_thread2 + 120 (queue.c:6628)
24  libsystem_pthread.dylib         0x000000018c94ba54 _pthread_wqthread + 216 (pthread.c:2336)
25  libsystem_pthread.dylib         0x000000018c951c7c start_wqthread + 8

Thread 1 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x0000000000000000   x5: 0x0000000000989680   x6: 0x0000000000000000   x7: 0x0000000000000b00
    x8: 0x000000016dc07000   x9: 0x0000000000000000  x10: 0x00000000000003e8  x11: 0x00000000000007fd
   x12: 0x0000000000000001  x13: 0x0000000087c1e021  x14: 0x0000000087e1e800  x15: 0x000000000000003d
   x16: 0x0000000000000148  x17: 0x00000001ca4c2f10  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000002407  x21: 0x000000016dc070e0  x22: 0x0000000000000000  x23: 0x0000000000000000
   x24: 0x0000000105f01790  x25: 0x00000001071198a0  x26: 0x0000000107872600  x27: 0x000000016dc05510
   x28: 0x0000000000000000   fp: 0x000000016dc04690   lr: 0x000000018c94ad10
    sp: 0x000000016dc04670   pc: 0x000000018ca2aefc cpsr: 0x40000000
   esr: 0x56000080  Address size fault

Environment

Microsoft Visual Studio Community 2019
Version 16.3.9
VisualStudio.16.Release/16.3.9+29509.3
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Community

Visual C++ 2019   00435-60000-00000-AA487
Microsoft Visual C++ 2019

Application Insights Tools for Visual Studio Package   9.1.00913.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2019   16.3.286.43615
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.3.286.43615
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.3.286.43615
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   16.3.286.43615
Azure Functions and Web Jobs Tools

C# Tools   3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub.VisualStudio   2.10.8.8132
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 0x10 - v2.9.20816.1

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.0.83+gbc8a4b23ec
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.3.7 (9d260c5)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   5.3.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.61908.27190
Microsoft SQL Server Data Tools

TypeScript Tools   16.0.10821.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.4 for F# 4.6   16.3.0-beta.19455.1+0422ff293bb2cc722fe5021b85ef50378a9af823
Microsoft Visual F# Tools 10.4 for F# 4.6

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.3.0.278 (d16-3@40034cd)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.3.0.256 (remotes/origin/d16-3@8a223bfd7)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.3.565 (27e9746)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   10.0.6.2 (d16-3/c407838)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: mono/mono/2019-06@476d72b9e32
    Java.Interop: xamarin/java.interop/d16-3@5836f58
    LibZipSharp: grendello/LibZipSharp/d16-3@71f4a94
    LibZip: nih-at/libzip/rel-1-5-1@b95cf3fd
    ProGuard: xamarin/proguard/master@905836d
    SQLite: xamarin/sqlite/3.27.1@8212a2d
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-3@cb41333

Xamarin.iOS and Xamarin.Mac SDK   13.6.0.12 (e3c2b40)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
chamons commented 4 years ago

I've tried this on iOS 13.1.3 and iOS 13.2.2 and can't reproduce the crash.

I do get:

2019-11-15 15:01:31.611 BGCrash[325:7302] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2019-11-15 15:01:35.750 BGCrash[325:7302] Can't end BackgroundTask: no background task exists with identifier 4 (0x4), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

Can you please attach a project showing the problem.

chamons commented 4 years ago

I just saw this, let me try in Release and archived, but that shouldn't affect much...

***PLEASE NOTE: This crash only occurs AFTER submitting app to the App Store, it does not happen when running the app locally.

chamons commented 4 years ago

Still not seeing it - Can you please attach a project?

FlxGen commented 4 years ago

I'll make a stripped down version for you. I should also note it crashes in TestFlight. I'll try an ad-hoc install. Perhaps there is a TestFlight bug.

FlxGen commented 4 years ago

I've tried this on iOS 13.1.3 and iOS 13.2.2 and can't reproduce the crash.

I do get:

2019-11-15 15:01:31.611 BGCrash[325:7302] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
2019-11-15 15:01:35.750 BGCrash[325:7302] Can't end BackgroundTask: no background task exists with identifier 4 (0x4), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

Can you please attach a project showing the problem.

I get this too. I think it has something to do with the debugger because it doesn't happen if no debugger is attached.

chamons commented 4 years ago

I tried release mode on phone no debugger as well. Nothing yet.

FlxGen commented 4 years ago

BackgroundCrash.zip

FlxGen commented 4 years ago

App attached. I put it in to TestFlight and it has the same behavior.

FlxGen commented 4 years ago

For whatever reason the stack trace has more detail.

0   libsystem_kernel.dylib          0x00000001811a0efc __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001810c0d10 pthread_kill + 196 (pthread.c:1456)
2   libsystem_c.dylib               0x0000000181050af4 __abort + 116 (abort.c:147)
3   libsystem_c.dylib               0x0000000181050a80 abort + 116 (abort.c:118)
4   BackgroundCrash                 0x00000001013a7dc4 xamarin_unhandled_exception_handler + 52 (runtime.m:1180)
5   BackgroundCrash                 0x00000001012c8fc8 mono_invoke_unhandled_exception_hook + 132
6   BackgroundCrash                 0x000000010126bf24 mono_handle_exception_internal + 5580
7   BackgroundCrash                 0x000000010126a94c mono_handle_exception + 56
8   BackgroundCrash                 0x0000000101261aa0 mono_arm_throw_exception + 376
9   BackgroundCrash                 0x0000000101199ebc throw_exception + 172
10  BackgroundCrash                 0x00000001013183bc mono_raise_exception + 64
11  BackgroundCrash                 0x00000001013a7b30 xamarin_process_managed_exception + 376 (runtime.m:2441)
12  BackgroundCrash                 0x00000001013a79ac xamarin_process_managed_exception_gchandle + 56 (runtime.m:1172)
13  BackgroundCrash                 0x00000001013a7968 xamarin_ftnptr_exception_handler + 24 (runtime.m:1161)
14  BackgroundCrash                 0x000000010126bc88 mono_handle_exception_internal + 4912
15  BackgroundCrash                 0x000000010126a94c mono_handle_exception + 56
16  BackgroundCrash                 0x0000000101261aa0 mono_arm_throw_exception + 376
17  BackgroundCrash                 0x0000000101199ebc throw_exception + 172
18  BackgroundCrash                 0x00000001010a67e4 System_Reflection_RuntimeConstructorInfo_DoInvoke_object_System_Reflection_BindingFlags_System_Re... + 452
19  BackgroundCrash                 0x00000001010a68f8 System_Reflection_RuntimeConstructorInfo_Invoke_System_Reflection_BindingFlags_System_Reflection_... + 56
20  BackgroundCrash                 0x000000010109af74 System_Reflection_ConstructorInfo_Invoke_object__ + 52
21  BackgroundCrash                 0x00000001011d8fc8 ObjCRuntime_Runtime_ConstructNSObject_T_REF_intptr_System_Type_ObjCRuntime_Runtime_MissingCtorRes... + 216 (Runtime.cs:1133)
22  BackgroundCrash                 0x00000001011d9be8 ObjCRuntime_Runtime_GetNSObject_T_REF_intptr + 376 (Runtime.cs:1274)
23  BackgroundCrash                 0x00000001011df108 ObjCRuntime_Trampolines_SDActionArity1V16_Invoke_intptr_intptr + 88 (Trampolines.g.cs:15219)
24  BackgroundCrash                 0x0000000101202408 wrapper_native_to_managed_ObjCRuntime_Trampolines_SDActionArity1V16_Invoke_intptr_intptr + 88
25  BackgroundTasks                 0x00000001aa424ed0 __41-[BGTaskScheduler _runTask:registration:]_block_invoke.206 + 224 (BGTaskScheduler.m:446)
26  libdispatch.dylib               0x000000018105ab7c _dispatch_call_block_and_release + 32 (init.c:1408)
27  libdispatch.dylib               0x000000018105bfd8 _dispatch_client_callout + 20 (object.m:495)
28  libdispatch.dylib               0x0000000181062450 _dispatch_lane_serial_drain + 568 (inline_internal.h:2484)
29  libdispatch.dylib               0x0000000181062eb4 _dispatch_lane_invoke + 424 (inline_internal.h:1851)
30  libdispatch.dylib               0x000000018106bf20 _dispatch_workloop_worker_thread + 580 (queue.c:6445)
31  libsystem_pthread.dylib         0x00000001810c1a94 _pthread_wqthread + 280 (pthread.c:2323)
32  libsystem_pthread.dylib         0x00000001810c7c7c start_wqthread + 8
FlxGen commented 4 years ago

Xcode 11.3 beta (11C24b) corrects this issue so it must have been an iOS bug.

FlxGen commented 4 years ago

I spoke too soon.

FlxGen commented 4 years ago

I found an line on the console while the app quits. This has to be a Xamarin bug.


Unhandled Exception: System.MemberAccessException: Cannot create an instance of BackgroundTasks.BGTask because it is an abstract class
chamons commented 4 years ago

Interesting, we'll have to dig into this....

FlxGen commented 4 years ago

Would it be possible to bind this class as a work around or would I just get the same object?

rolfbjarne commented 4 years ago

This is a(nother) concrete example of why we need a fix for #4969.

FlxGen commented 4 years ago
  1. Is there a work around? (I'd really love to use this class)
  2. Why doesn't it happen with local installs?
rolfbjarne commented 4 years ago
  1. Is there a work around? (I'd really love to use this class)

Unfortunately I don't think there is a workaround, this is something we'll have to fix on our side.

  1. Why doesn't it happen with local installs?

Most likely AppStore installs hit another codepath inside iOS, where iOS uses a different subclass of BGTask (a private subclass) to do the work.

FlxGen commented 4 years ago

The issue is over a year old. Will this be fixed soon or should I use another method? BGTaskRequest is a great new feature and works MUCH (can't emphasize this enough via web browser) better than Background Fetch. That is to say, it would be very nice to have.

FlxGen commented 4 years ago

Please note, BGTaskRequest did initially work. It was broken during one of Apples many Xcode and iOS13 updates since 13 was released. Not sure which broke it.

rolfbjarne commented 4 years ago

Will this be fixed soon or should I use another method?

I would advice to use another method if you can, because even if we fixed the issue today, it would take months for it to reach a stable release.

donchakkappan commented 4 years ago

@rolfbjarne @FlxGen @chamons Could you please have an update on this.A similar issue is here : https://github.com/xamarin/ios-samples/issues/392

"Refreshing and Maintaining Your App Using Background Tasks" this is not working.Apple's official swift version is working.Is there any solutions or workaround.People are eagerly waiting for this.

FlxGen commented 4 years ago

@donchakkappan Not sure if this is related. I get the callback, it just crashes when a BGTask is launched.

I agree with your assessment of the feature. The new background task scheduler is excellent compared to fetch. This should be escalated and fixed asap.

FlxGen commented 4 years ago

Most likely AppStore installs hit another codepath inside iOS, where iOS uses a different subclass of BGTask (a private subclass) to do the work.

Out of curiosity I tried an Ad-Hoc install with the same result. It must be the Mac host changing the codepath.

brminnick commented 4 years ago

Workaround

@FlxGen As the team continues investigating/fixing this bug, here's a workaround.

We can continue to use iOS 7-12's background fetch implementation: https://devblogs.microsoft.com/xamarin/add-background-data-refresh-to-ios-apps/

Apple marked it as deprecated, but it still works on iOS 13. Note that you'll have to remove BGTaskSchedulerPermittedIdentifiers from Info.plist.

Here's a sample demonstrating it on iOS 13: https://github.com/brminnick/BGBackgroundTaskWorkaround

public class AppDelegate : UIApplicationDelegate
{
    public override UIWindow? Window { get; set; }

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
        return true;
    }

    public override async void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
    {
        var alertController = new UIAlertController
        {
            Title = "Background Fetch Triggered",
            Message = "It's working!",
        };

        var currentViewController = Xamarin.Essentials.Platform.GetCurrentUIViewController();

        await currentViewController.PresentViewControllerAsync(alertController, true);

        completionHandler(UIBackgroundFetchResult.NoData);
    }
}

https://github.com/brminnick/BGBackgroundTaskWorkaround

mandel-macaque commented 4 years ago

API fixed, I'll test the application, if works as expected I'll backport the fix to d16-7 to be released in preview 2

FlxGen commented 4 years ago

@mandel-macaque Excellent, thanks!!!

mandel-macaque commented 4 years ago

@FlxGen can you please try the following pkg

If it fix your issue I'll merge https://github.com/xamarin/xamarin-macios/pull/8532

FlxGen commented 4 years ago

@mandel-macaque I will. I'll have an answer in a day or two.

FlxGen commented 4 years ago

@mandel-macaque I've never installed this before. Is there a doc with steps?

mandel-macaque commented 4 years ago

@FlxGen you just need to install it like a normal pkg and it will be used by default (you will need to re-start vsmac).

I do use the command line as follows, but you can simply double click:

sudo installer -pkg /path/to/the/downloaded.pkg -target /
mandel-macaque commented 4 years ago

@FlxGen any updates on this?

FlxGen commented 4 years ago

@mandel-macaque The issue appears to be resolved. Many thanks.

mandel-macaque commented 4 years ago

@FlxGen thanks for the confirmation, just backport it to d16-7. The fix will be in the next available preview.

brminnick commented 4 years ago

FYI - this fix is now available in both the current Stable and Preview Channels, in Xamarin.iOS v13.18.2.1 & Xamarin.iOS v13.20.0.22

Bhauraj001 commented 2 years ago

This issue is still reproducible with iOS 12 and below.