Skip to content

Commit d933d43

Browse files
committed
[WIP] Use mono_unhandled_exception for NET6
1 parent 21751b5 commit d933d43

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj)
242242
}
243243

244244
static Action<Exception> mono_unhandled_exception = null!;
245+
#if !NETCOREAPP
245246
static Action<AppDomain, UnhandledExceptionEventArgs> AppDomain_DoUnhandledException = null!;
247+
#endif
246248

247249
static void Initialize ()
248250
{
@@ -253,6 +255,7 @@ static void Initialize ()
253255
mono_unhandled_exception = (Action<Exception>) Delegate.CreateDelegate (typeof(Action<Exception>), mono_UnhandledException);
254256
}
255257

258+
#if !NETCOREAPP
256259
if (AppDomain_DoUnhandledException == null) {
257260
var ad_due = typeof (AppDomain)
258261
.GetMethod ("DoUnhandledException",
@@ -265,8 +268,14 @@ static void Initialize ()
265268
typeof (Action<AppDomain, UnhandledExceptionEventArgs>), ad_due);
266269
}
267270
}
271+
#endif
268272
}
269273

274+
#if NETCOREAPP
275+
[DllImport (AndroidRuntime.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
276+
extern static void monodroid_unhandled_exception (Exception javaException);
277+
#endif
278+
270279
internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPtr, IntPtr javaExceptionPtr)
271280
{
272281
if (!PropagateExceptions)
@@ -287,14 +296,18 @@ internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPt
287296
try {
288297
var jltp = javaException as JavaProxyThrowable;
289298
Exception? innerException = jltp?.InnerException;
290-
var args = new UnhandledExceptionEventArgs (innerException ?? javaException, isTerminating: true);
291299

292300
Logger.Log (LogLevel.Info, "MonoDroid", "UNHANDLED EXCEPTION:");
293301
Logger.Log (LogLevel.Info, "MonoDroid", javaException.ToString ());
294302

303+
#if !NETCOREAPP
304+
var args = new UnhandledExceptionEventArgs (innerException ?? javaException, isTerminating: true);
295305
// Disabled until Linker error surfaced in https://github.com/xamarin/xamarin-android/pull/4302#issuecomment-596400025 is resolved
296306
//AppDomain.CurrentDomain.DoUnhandledException (args);
297307
AppDomain_DoUnhandledException?.Invoke (AppDomain.CurrentDomain, args);
308+
#else
309+
monodroid_unhandled_exception (innerException ?? javaException);
310+
#endif
298311
} catch (Exception e) {
299312
Logger.Log (LogLevel.Error, "monodroid", "Exception thrown while raising AppDomain.UnhandledException event: " + e.ToString ());
300313
}

src/monodroid/jni/monodroid-glue-internal.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ namespace xamarin::android::internal
174174
}
175175

176176
#if defined (NET6)
177+
void unhandled_exception (MonoObject *java_exception)
178+
{
179+
mono_unhandled_exception (java_exception);
180+
}
181+
177182
void propagate_uncaught_exception (JNIEnv *env, jobject javaThread, jthrowable javaException);
178183
#else // def NET6
179184
void propagate_uncaught_exception (MonoDomain *domain, JNIEnv *env, jobject javaThread, jthrowable javaException);

src/monodroid/jni/pinvoke-override-api.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ monodroid_get_dylib (void)
345345
return nullptr;
346346
}
347347

348+
static void
349+
monodroid_unhandled_exception (MonoObject *java_exception)
350+
{
351+
monodroidRuntime.unhandled_exception (java_exception);
352+
}
353+
348354
#define PINVOKE_SYMBOL(_sym_) { #_sym_, reinterpret_cast<void*>(&_sym_) }
349355

350356
MonodroidRuntime::pinvoke_api_map MonodroidRuntime::xa_pinvoke_map = {
@@ -583,6 +589,7 @@ MonodroidRuntime::pinvoke_api_map MonodroidRuntime::xa_pinvoke_map = {
583589
PINVOKE_SYMBOL (recv_uninterrupted),
584590
PINVOKE_SYMBOL (send_uninterrupted),
585591
PINVOKE_SYMBOL (set_world_accessable),
592+
PINVOKE_SYMBOL (monodroid_unhandled_exception),
586593
};
587594

588595
MonodroidRuntime::pinvoke_library_map MonodroidRuntime::other_pinvoke_map (MonodroidRuntime::LIBRARY_MAP_INITIAL_BUCKET_COUNT);

0 commit comments

Comments
 (0)