Commit 9ad492a
authored
[NativeAOT] Add DefaultUncaughtExceptionHandler (#9994)
Context: dotnet/runtime#102730
Context: 1aa0ea7
What should happen when an exception is thrown and not caught?
partial class MainActivity {
protected override void OnCreate(Bundle? savedInstanceState) =>
throw new Exception("Uncaught exception");
}
What *previously* happened is that the app would exit, with an
`AndroidRuntime` tag containing the Java-side exception, which will
contain *some* managed info courtesy of 1aa0ea7.
I ActivityManager: Start proc 6911:net.dot.hellonativeaot/u0a205 for top-activity {net.dot.hellonativeaot/my.MainActivity}
…
D NativeAotRuntimeProvider: NativeAotRuntimeProvider()
D NativeAotRuntimeProvider: NativeAotRuntimeProvider.attachInfo(): calling JavaInteropRuntime.init()…
D JavaInteropRuntime: Loading NativeAOT.so...
I JavaInteropRuntime: JNI_OnLoad()
…
D NativeAotRuntimeProvider: NativeAotRuntimeProvider.onCreate()
D NativeAOT: Application..ctor(7fff01a958, DoNotTransfer)
D NativeAOT: Application.OnCreate()
…
D NativeAOT: MainActivity.OnCreate()
…
D NativeAOT: MainActivity.OnCreate() ColorStateList: ColorStateList{mThemeAttrs=nullmChangingConfigurations=0mStateSpecs=[[0, 1]]mColors=[0, 1]mDefaultColor=0}
D AndroidRuntime: Shutting down VM
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: net.dot.hellonativeaot, PID: 6911
E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.InvalidOperationException: What happened?
E AndroidRuntime: at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0x2f4
E AndroidRuntime: at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) + 0xc8
E AndroidRuntime: at my.MainActivity.n_onCreate(Native Method)
E AndroidRuntime: at my.MainActivity.onCreate(MainActivity.java:28)
E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:8595)
E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:8573)
E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3805)
E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3963)
E AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2484)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:205)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:294)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8225)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:573)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
W ActivityTaskManager: Force finishing activity net.dot.hellonativeaot/my.MainActivity
What *won't* happen is that the [`AppDomain.UnhandledException`][0]
event will ***not*** be raised
This is less than ideal, and will cause the
`InstallAndRunTests.SubscribeToAppDomainUnhandledException()` test
to fail, once that test is enabled for NativeAOT.
*Begin* to address this by setting
`Java.Lang.Thread.DefaultUncaughtExceptionHandler` to a
`Thread.IUncaughtExceptionHandler` instance which at least prints the
exception to `adb logcat`.
Update `samples/NativeAOT` to demonstrate this, by using a new
boolean `thrown` extra; if true, then `OnCreate()` throws:
adb shell am start --ez throw 1 net.dot.hellonativeaot/my.MainActivity
`adb logcat` continues to have the `FATAL EXCEPTION` message from
`AndroidRuntime`, as shown above, and `adb logcat` now also contains:
F DOTNET : FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: What happened?
F DOTNET : at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0x2f4
F DOTNET : at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) + 0xc8
which prints out the managed exception that we would expect to be
raised by `AppDomain.UnhandledException`, once that integration works.
TODO: once dotnet/runtime#102730 is fixed, update
`UncaughtExceptionMarshaler` to do whatever it needs to do to cause
the `AppDomain.UnhandledException` event to be raised.
Update `JavaInteropRuntime.init()` to marshal excpetions back to Java,
so that the process appropriately terminates if `init()` fails.
[0]: https://learn.microsoft.com/dotnet/api/system.appdomain.unhandledexception?view=net-9.01 parent 196d7a0 commit 9ad492a
File tree
3 files changed
+33
-0
lines changed- samples/NativeAOT
- src/Microsoft.Android.Runtime.NativeAOT
- Android.Runtime.NativeAOT
3 files changed
+33
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| 62 | + | |
56 | 63 | | |
| 64 | + | |
57 | 65 | | |
58 | 66 | | |
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments