Skip to content

mono_gc_run_finalize iOS crash #107641

Closed
Closed

Description

@timmac-qmc

Description

I'm getting some crash reports from a small number of users when opening our MAUI app on iOS with the following stack trace:

0   libsystem_kernel.dylib        	0x00000001e74eb42c __pthread_kill + 8 (:-1)
1   libsystem_pthread.dylib       	0x00000001fb28ac0c pthread_kill + 268 (pthread.c:1721)
2   libsystem_c.dylib             	0x00000001a638eba0 abort + 180 (abort.c:118)
3   QMCMobile                     	0x00000001031f29ac 0x102a80000 + 7809452
4   QMCMobile                     	0x000000010317630c mono_runtime_setup_stat_profiler + 0 (mini-posix.c:662)
5   libsystem_platform.dylib      	0x00000001fb1cee9c _sigtramp + 56 (sigtramp.c:116)
6   libsystem_pthread.dylib       	0x00000001fb28ac0c pthread_kill + 268 (pthread.c:1721)
7   libsystem_c.dylib             	0x00000001a638eba0 abort + 180 (abort.c:118)
8   QMCMobile                     	0x0000000102ea9dd4 print_callback(char const*, int) + 0 (runtime.m:1201)
9   QMCMobile                     	0x000000010302bfd0 monoeg_g_logv_nofree + 212 (goutput.c:167)
10  QMCMobile                     	0x000000010302c040 monoeg_assertion_message + 40 (goutput.c:207)
11  QMCMobile                     	0x00000001030f4bac mono_unhandled_exception_checked + 0 (object.c:4540)
12  QMCMobile                     	0x00000001031030c4 mono_thread_internal_unhandled_exception + 48 (threads.c:4673)
13  QMCMobile                     	0x0000000103125ec4 mono_gc_run_finalize + 684 (gc.c:352)
14  QMCMobile                     	0x0000000103050624 sgen_gc_invoke_finalizers + 192 (sgen-gc.c:2852)
15  QMCMobile                     	0x00000001031270d8 finalizer_thread + 776 (gc.c:905)
16  QMCMobile                     	0x000000010310395c start_wrapper + 352 (threads.c:1271)
17  libsystem_pthread.dylib       	0x00000001fb28906c _pthread_start + 136 (pthread.c:931)
18  libsystem_pthread.dylib       	0x00000001fb2840d8 thread_start + 8 (:-1)

Complete crash report attached
2024-09-05_11-36-23.7199_+0100-a81f98656fd14cd6ee1031f7089dfc2e6b30f05c.txt

Steps to Reproduce

Unknown

Link to public reproduction project repository

No response

Version with bug

8.0.82 SR8.2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 17.*+

Did you find any workaround?

No

Relevant log output

No response

Activity

  1. github-actions commented on Sep 5, 2024

    @github-actions
    Contributor

    Hi I'm an AI powered bot that finds similar issues based off the issue title.

    Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

    Open similar issues:

    Closed similar issues:

    Note: You can give me feedback by thumbs upping or thumbs downing this comment.

  2. deleted a comment from timmac-qmc on Sep 5, 2024
  3. drasticactions commented on Sep 6, 2024

    @drasticactions

    This could be an issue in the runtime, considering that it seems that something is throwing in mono_gc_run_finalize. This may not be a MAUI UI specific issue. @rolfbjarne What do you think?

  4. rolfbjarne commented on Sep 6, 2024

    @rolfbjarne
    Member

    The problem is that a finalizer threw an exception:

    11  QMCMobile                     	0x00000001030f4bac mono_unhandled_exception_checked + 0 (object.c:4540)
    12  QMCMobile                     	0x00000001031030c4 mono_thread_internal_unhandled_exception + 48 (threads.c:4673)
    13  QMCMobile                     	0x0000000103125ec4 mono_gc_run_finalize + 684 (gc.c:352)
    

    The trick is figuring out more information about the exception.

    My advice would be to add event listeners to both of these:

    And log/upload any results to your telemetry solution of choice.

  5. timmac-qmc commented on Sep 7, 2024

    @timmac-qmc
    Author

    The problem is that a finalizer threw an exception:

    11  QMCMobile                     	0x00000001030f4bac mono_unhandled_exception_checked + 0 (object.c:4540)
    12  QMCMobile                     	0x00000001031030c4 mono_thread_internal_unhandled_exception + 48 (threads.c:4673)
    13  QMCMobile                     	0x0000000103125ec4 mono_gc_run_finalize + 684 (gc.c:352)
    

    The trick is figuring out more information about the exception.

    My advice would be to add event listeners to both of these:

    And log/upload any results to your telemetry solution of choice.

    Implemented and nothing appears to be getting captured...

  6. timmac-qmc commented on Sep 9, 2024

    @timmac-qmc
    Author

    May be unrelated but after trawling through logs the following is showing consistently:

    QMCMobile[4349]/1#6 LF=0 copy_matching Error Domain=NSOSStatusErrorDomain Code=-34018 "Client explicitly specifies access group com.microsoft.adalcache but is only entitled for (
        "2KLF58357X.com.microsoft.adalcache",
        "2KLF58357X.com.qmc.qmcmobile"
    )" UserInfo={numberOfErrorsDeep=0, NSDescription=Client explicitly specifies access group com.microsoft.adalcache but is only entitled for (
        "2KLF58357X.com.microsoft.adalcache",
        "2KLF58357X.com.qmc.qmcmobile"
    )}
    

    I'm using the following code as given by the documentation so I'm not sure why this error is occurring?
    Documentation clearly states that TeamID is prepended to the security group string.

    #elif IOS
                    authenticationClient = PublicClientApplicationBuilder
                        .Create(_azureADConfig.ClientId)
                        .WithAuthority(AzureCloudInstance.AzurePublic, _azureADConfig.TenantId) // Only allow accounts in the tenant to authenticate
                        .WithIosKeychainSecurityGroup("com.microsoft.adalcache")
                        .WithRedirectUri($"msal{_azureADConfig.ClientId}://auth")
                        .Build();
    #else
    

    EDIT:
    Seems I'm not the first user to encounter this:

    AzureAD/microsoft-authentication-library-for-dotnet#1562

  7. rolfbjarne commented on Sep 9, 2024

    @rolfbjarne
    Member

    May be unrelated but after trawling through logs the following is showing consistently:

    QMCMobile[4349]/1#6 LF=0 copy_matching Error Domain=NSOSStatusErrorDomain Code=-34018 "Client explicitly specifies access group com.microsoft.adalcache but is only entitled for (
        "2KLF58357X.com.microsoft.adalcache",
        "2KLF58357X.com.qmc.qmcmobile"
    )" UserInfo={numberOfErrorsDeep=0, NSDescription=Client explicitly specifies access group com.microsoft.adalcache but is only entitled for (
        "2KLF58357X.com.microsoft.adalcache",
        "2KLF58357X.com.qmc.qmcmobile"
    )}
    

    I'm using the following code as given by the documentation so I'm not sure why this error is occurring? Documentation clearly states that TeamID is prepended to the security group string.

    #elif IOS
                    authenticationClient = PublicClientApplicationBuilder
                        .Create(_azureADConfig.ClientId)
                        .WithAuthority(AzureCloudInstance.AzurePublic, _azureADConfig.TenantId) // Only allow accounts in the tenant to authenticate
                        .WithIosKeychainSecurityGroup("com.microsoft.adalcache")
                        .WithRedirectUri($"msal{_azureADConfig.ClientId}://auth")
                        .Build();
    #else
    

    EDIT: Seems I'm not the first user to encounter this:

    AzureAD/microsoft-authentication-library-for-dotnet#1562

    This is not the direct cause of the crash (it may cause other issues in your app though; and technically it may even be an indirect cause of the crash if it's causing exceptions on the finalizer thread).

  8. timmac-qmc commented on Sep 9, 2024

    @timmac-qmc
    Author

    This is not the direct cause of the crash (it may cause other issues in your app though; and technically it may even be an indirect cause of the crash if it's causing exceptions on the finalizer thread).

    I'm at a bit of a loss here, nothing seems to be getting captured or reported. Is there anything else you can suggest to track down the cause?

    I'm using the code below to try and catch everything but nothing shows in the device console:

            {
               AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
               TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
    #if IOS
                Runtime.MarshalManagedException += (object sender, MarshalManagedExceptionEventArgs args) =>
                {
                    Console.WriteLine("Marshalling managed exception");
                    Console.WriteLine("    Exception: {0}", args.Exception);
                    Console.WriteLine("    Mode: {0}", args.ExceptionMode);
                };
                Runtime.MarshalObjectiveCException += (object sender, MarshalObjectiveCExceptionEventArgs args) =>
                {
                    Console.WriteLine("Marshalling Objective-C exception");
                    Console.WriteLine("    Exception: {0}", args.Exception);
                    Console.WriteLine("    Mode: {0}", args.ExceptionMode); ;
                };
    #endif
            }
    
            private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
            {
                Send(e.Exception);
            }
    
            private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                try
                {
                    var message = e.ExceptionObject.ToString();
                    if (message is not null)
                        Send(message);
                }
                catch(Exception ex) {
                    Send(ex);
                }
            }
    
            public void Send(Exception ex)
            {
                Console.WriteLine($"Exception: {ex}");
            }
    
  9. rolfbjarne commented on Sep 9, 2024

    @rolfbjarne
    Member

    Can you share the device log for a crash + the corresponding crash report?

  10. timmac-qmc commented on Sep 10, 2024

    @timmac-qmc
    Author

    Can you share the device log for a crash + the corresponding crash report?

    Just noticed in the log

    Could not find System.Runtime.InteropServices.JavaScript referenced by assembly QMCMobile, Version=2.0.7.0, Culture=neutral, PublicKeyToken=null.

  11. rolfbjarne commented on Sep 10, 2024

    @rolfbjarne
    Member

    Can you share the device log for a crash + the corresponding crash report?

    Just noticed in the log

    Could not find System.Runtime.InteropServices.JavaScript referenced by assembly QMCMobile, Version=2.0.7.0, Culture=neutral, PublicKeyToken=null.

    That could be a good place to start looking, although it's strange that this didn't cause a build failure.

    I didn't find anything else in the log, so if that doesn't lead anywhere, can you zip up the .app that's installed on your device and attach it / link to it here (it should be somewhere inside the bin directory of your project folder)?

  12. timmac-qmc commented on Sep 10, 2024

    @timmac-qmc
    Author

    Can you share the device log for a crash + the corresponding crash report?

    Just noticed in the log
    Could not find System.Runtime.InteropServices.JavaScript referenced by assembly QMCMobile, Version=2.0.7.0, Culture=neutral, PublicKeyToken=null.

    That could be a good place to start looking, although it's strange that this didn't cause a build failure.

    I didn't find anything else in the log, so if that doesn't lead anywhere, can you zip up the .app that's installed on your device and attach it / link to it here (it should be somewhere inside the bin directory of your project folder)?

    Resolved using config below, app still crashes.

    <ItemGroup>
    	<TrimmerRootAssembly Include="System.Runtime.InteropServices.JavaScript" />
    </ItemGroup>
    
    

    No crash report is currently being generated on the device.
    crash_log.txt

    QMCMobile.app.zip

  13. rolfbjarne commented on Sep 10, 2024

    @rolfbjarne
    Member

    QMCMobile.app.zip

    All files in this zip are zero-sized, so I assume you're building from Windows.

    In that case, you need to get the real app contents from your macOS machine.

    It's somewhere in this directory:

    ~/Library/Caches/Xamarin/mtbs/builds/(project name)

  14. timmac-qmc commented on Sep 10, 2024

    @timmac-qmc
    Author

    .app

    Folder structure is a bit different but hopefully this is what you need:

  15. rolfbjarne commented on Sep 10, 2024

    @rolfbjarne
    Member

    .app

    Folder structure is a bit different but hopefully this is what you need: mega.nz/file/XYAkRLZT#X1q71AHgPB-SPRWX5eLdclC-3_xJrBTlCZJ6U7QVFYg

    TIL that I can't access mega.nz for security reasons.

    Is there any possibility you can use another file sharing service?

  16. 4 remaining items

  17. transferred this issue fromdotnet/mauion Sep 10, 2024
  18. self-assigned this
    on Sep 11, 2024
  19. dotnet-policy-service commented on Sep 11, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  20. removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Sep 12, 2024
  21. Flash3001 commented on May 15, 2025

    @Flash3001

    @rolfbjarne What is the status of this issue? There is a pull request #110863 that was released on 9.0.200 that mentions this issue. But this ticket is still opened.

    I started having this problem (or very similar) after updating to 9.0.200 though.

  22. rolfbjarne commented on May 19, 2025

    @rolfbjarne
    Member

    @rolfbjarne What is the status of this issue? There is a pull request #110863 that was released on 9.0.200 that mentions this issue. But this ticket is still opened.

    I started having this problem (or very similar) after updating to 9.0.200 though.

    We never got a test project we could use to reproduce this. Since you're seeing the same problem, would you be able to provide a test project?

  23. timmac-qmc commented on May 22, 2025

    @timmac-qmc
    Author

    Apologies I never got back to this due to time constraints.
    I never found the root cause but managed to work around the crash by using the following project configuration

      <PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
        <UseInterpreter>true</UseInterpreter>
      </PropertyGroup>
      <ItemGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
        <TrimmerRootAssembly Include="System.Runtime.InteropServices.JavaScript" />
      </ItemGroup>
    
  24. BrzVlad commented on Jun 11, 2025

    @BrzVlad
    Member

    Doesn't seem we can address this without a repro. Feel free to reopen if you manage to reproduce this in a sample application.

  25. locked and limited conversation to collaborators on Jul 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions