Alternative to Mono.Runtime.RemoveSignalHandlers/InstallSignalHandlers

Alexander 1 Reputation point
2022-09-15T10:06:11.06+00:00

We have Xamarin.Android and Xamarin.iOS apps that can catch and report native crashes by installing their own signal handlers like this:

Mono.Runtime.RemoveSignalHandlers();  
  
try  
{  
    NativeCrashListener.Initialize();  
}  
finally  
{  
    Mono.Runtime.InstallSignalHandlers();  
}  

Migrating from Xamarin to .NET 6 I noticed that the Mono.Runtime type is not accessible any more, even though the Mono runtime is still there for Android and iOS, so obviously, you can't remove and install the handlers like this.

Mono suggests the following alternative:

IntPtr sigbus = Marshal.AllocHGlobal (512);  
IntPtr sigsegv = Marshal.AllocHGlobal (512);  
  
// Store Mono's SIGSEGV and SIGBUS handlers  
sigaction (Signal.SIGBUS, IntPtr.Zero, sigbus);  
sigaction (Signal.SIGSEGV, IntPtr.Zero, sigsegv);  
  
// Enable crash reporting libraries  
EnableCrashReporting ();  
  
// Restore Mono SIGSEGV and SIGBUS handlers  
sigaction (Signal.SIGBUS, sigbus, IntPtr.Zero);  
sigaction (Signal.SIGSEGV, sigsegv, IntPtr.Zero);  
  
Marshal.FreeHGlobal (sigbus);  
Marshal.FreeHGlobal (sigsegv);  

We tried both the way Mono recommends and getting rid of the remove/install code at all on our Xamarin apps to see if it makes a difference. Turns out that it does, and the number of crashes that we get significantly differs in both cases. It is hard to say which way is more correct though, since you can only see the difference on big numbers, but in any case the behavior is not the same.

I would still want to apply something similar to the .NET 6 apps, and I wonder whether there is an alternative API I didn't find. I know that there is some API to work with POSIX signals now in .NET 6, so maybe I'm just missing something.

Can anyone advise an alternative or say with confidence that .NET 6 doesn't need such handlers detach at all?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,859 questions
{count} votes