Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[One .NET] fix code path for unhandled exceptions (#4878)
Context: dotnet/runtime#38465 When a thread throws an exception which isn't caught, we (eventually) hit the "unhandled exception" codepath of `JNIEnv.PropagateUncaughtException()` (2aff4e7 & others), which relies on the following methods which are not present in .NET 5: * `System.Diagnostics.Debugger.Mono_UnhandledException()` * `AppDomain.DoUnhandledException()` Unfortunately, we didn't check that the `MethodInfo`s for these methods were `null` before invoking them, which means under .NET 5 we would throw an exception while attempting to deal with an already "in-flight" exception, i.e. we'd throw a *nested* exception! Unable to initialize UncaughtExceptionHandler. Nested exception caught: System.ArgumentNullException: Value cannot be null. (Parameter 'method') at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure) at System.Delegate.CreateDelegate(Type type, MethodInfo method) at Android.Runtime.JNIEnv.Initialize() at Android.Runtime.JNIEnv.PropagateUncaughtException(IntPtr env, IntPtr javaThreadPtr, IntPtr javaExceptionPtr) or: Exception thrown while raising AppDomain.UnhandledException event: System.NullReferenceException: Object reference not set to an instance of an object at Android.Runtime.JNIEnv.PropagateUncaughtException(IntPtr env, IntPtr javaThreadPtr, IntPtr javaExceptionPtr) Add appropriate `null` checks in `JNIEnv.PropagateUncaughtException()` to avoid generation of nested exceptions. TODO: Work with the dotnet/mono teams so that we can reintroduce the functionality of `AppDomain.DoUnhandledException()`/etc..
- Loading branch information