Skip to content

[NativeAOT] Raise AppDomain.UnhandledException event #10092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025

Conversation

jonpryor
Copy link
Contributor

Context: 9ad492a
Context: dotnet/runtime#102730

Commit 9ad492a had a TODO:

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.

dotnet/runtime#102730 has been fixed.

Update UncaughtExceptionMarshaler to call
ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(object) with the unhandled exception, so that the
AppDomain.UnhandledException event is raised.

Update samples/NativeAOT to subscribe to the
AppDomain.UnhandledException event.

Install the app:

./dotnet-local.sh build -t:Install samples/NativeAOT/NativeAOT.csproj

and run the sample so that it throws an unhandled exception:

adb shell am start --ez throw 1 net.dot.hellonativeaot/my.MainActivity

and now adb logcat contains:

I NativeAotFromAndroid: AppDomain.UnhandledException!
I NativeAotFromAndroid:   sender:  [False ]
I NativeAotFromAndroid:   e.IsTerminating: True
I NativeAotFromAndroid:   e.ExceptionObject: System.InvalidOperationException: What happened?
I NativeAotFromAndroid:    at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0x2f4
I NativeAotFromAndroid:    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) + 0xc8

showing that the AppDomain.UnhandledException event was raised.

Context: 9ad492a
Context: dotnet/runtime#102730

Commit 9ad492a had a TODO:

> 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.

dotnet/runtime#102730 *has* been fixed.

Update `UncaughtExceptionMarshaler` to call
`ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(object)`
with the unhandled exception, so that the
`AppDomain.UnhandledException` event is raised.

Update `samples/NativeAOT` to subscribe to the
`AppDomain.UnhandledException` event.

Install the app:

	./dotnet-local.sh build -t:Install samples/NativeAOT/NativeAOT.csproj

and run the sample so that it throws an unhandled exception:

	adb shell am start --ez throw 1 net.dot.hellonativeaot/my.MainActivity

and now `adb logcat` contains:

	I NativeAotFromAndroid: AppDomain.UnhandledException!
	I NativeAotFromAndroid:   sender:  [False ]
	I NativeAotFromAndroid:   e.IsTerminating: True
	I NativeAotFromAndroid:   e.ExceptionObject: System.InvalidOperationException: What happened?
	I NativeAotFromAndroid:    at NativeAOT.MainActivity.OnCreate(Bundle savedInstanceState) + 0x2f4
	I NativeAotFromAndroid:    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) + 0xc8

showing that the `AppDomain.UnhandledException` event was raised.
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the behavior of unhandled exceptions for NativeAOT by ensuring that the AppDomain.UnhandledException event is raised.

  • Updated UncaughtExceptionMarshaler.cs to call ExceptionHandling.RaiseAppDomainUnhandledExceptionEvent(e)
  • Modified samples/NativeAOT/MainApplication.cs to subscribe to the AppDomain.UnhandledException event and log relevant details

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Microsoft.Android.Runtime.NativeAOT/UncaughtExceptionMarshaler.cs Replace the commented-out call with a call to raise the AppDomain.UnhandledException event.
samples/NativeAOT/MainApplication.cs Add an event subscription for AppDomain.CurrentDomain.UnhandledException.
Comments suppressed due to low confidence (1)

src/Microsoft.Android.Runtime.NativeAOT/UncaughtExceptionMarshaler.cs:19

  • There is an inconsistency in the naming of the exception variable: the logging and event-raising use 'e' whereas the original handler is called with 'exception'. Ensure the same variable name is used consistently throughout the method.
OriginalHandler?.UncaughtException (thread, exception);

@jonpryor jonpryor merged commit a8a573c into main Apr 30, 2025
59 checks passed
@jonpryor jonpryor deleted the dev/jonp/jonp-naot-forward-unhandled-exceptions branch April 30, 2025 23:00
@github-actions github-actions bot locked and limited conversation to collaborators May 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants