1

We have a Xamarin iOS application that sits on top of a heavily async PCL library. There is a chance that occasionally a Task will fault in the library and not be observed. We have, therefore, wired up the UnobservedTaskException handler on the TaskScheduler to ensure this doesn't necessarily bring our app down.

This worked fine until we enabled HockeyApp for centralising crash reporting. Hockey adds its own handler for unobserved task exceptions which always terminates the app after sending a crash report to its servers. I have no problem with them doing this for the AppDomain UnhandledException handler but I need to stop them killing the app on unobserved task exceptions.

There appears to be a mechanism for installing a custom handler for these exceptions but I cannot see how to install it in the Xamarin iOS Hockey SDK

Here is the code we use to enable Hockey

var manager = BITHockeyManager.SharedHockeyManager;

manager.Configure(APPID);
#if DEBUG
   manager.DebugLogEnabled = true;
#endif

manager.StartManager();

Does anyone know how to override HockeyApp's default behaviour for UnobservedTaskExceptions?

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23

3 Answers3

3

The UnobservedTaskException EventHandler is hard-coded in the StartManager method with no direct way to remove.

TaskScheduler.UnobservedTaskException += (sender, e) => ThrowExceptionAsNative(e.Exception);

Ref: https://github.com/bitstadium/HockeySDK-Xamarin/blob/28e67ecba14d00c8bea8043e08678af6044d33cf/source/HockeySDK.iOSBindings/Additions.cs#L43

Personally, I build from source, publicly expose ThrowExceptionAsNative and add configuration methods to include/exclude the default handler when StartManager is called. This is just like HockeyApp does with the HockeySDK-Windows api.

Thus when catching an UnobservedTaskException you have an option to handle it yourself or to throw it as a native exception.

This is much like the original Xamarin native bindings and I do not understand why they did it this way as in the HockeySDK-Windows code, they removed the default handling of UnobservedTaskException:

Since .NET 4.5, by default, UnobservedTaskExceptions do no longer cause the app to crash. The SDK has not been adapted for this and still logs these errors and causes the program to exit, though this might not be needed or intended.

Users who wish to continue using the handler, should add calls to RegisterUnobservedTaskExceptionHandler() or RegisterDefaultUnobservedTaskExceptionHandler() after calling Configure().

For a couple of clients who do not want a custom build of HockeySDK.Xamarin, I do the EventHandler removal via reflection after the StartManager call and add in our custom handler. Using this approach you will not have a public ThrowExceptionAsNative available to throw the exception as a native one if needed, but some more reflection can do it :-/

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
1

According to this support question, the answer is no.

https://support.hockeyapp.net/discussions/problems/60521-hockeysdkuwp-413-no-way-to-disable-unobservedtaskexceptions

And this commit appears to be trying to fix the issue

https://github.com/bitstadium/HockeySDK-Windows/commit/1c1bc9715e64dd1283b3dc5db40ccdc9e59f4fc3

Matthew Adams
  • 464
  • 3
  • 6
  • Thanks Matthew - it looks like there may have been ways to do this at some point in the past https://github.com/bitstadium/HockeySDK-Windows/blob/1520efdfab0b13145b0282bd23650988e417a2bd/HockeySDK_Win81/HockeyClientExtensionsWin81.cs#L34 but that may have only been for Win81 store apps. – Richard Blewett Oct 15 '16 at 07:42
  • @RichardBlewett `HockeySDK-Windows` != `HockeySDK-Xamarin` With `HockeySDK-Windows` you can remove the handler via `RegisterUnobservedTaskExceptionHandler` but there is no parallel in `HockeySDK-Xamarin`. See my answer for more details – SushiHangover Oct 15 '16 at 08:53
0

I solved this problem with this line of code:

var ex = t.Exception;

when the Task throw the exception i take the result

if ((t.IsFaulted) || (t.IsCanceled))
{
    var ex = t.Exception;