-
Notifications
You must be signed in to change notification settings - Fork 534
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
Using the default TrimMode=link breaks FirstChanceException handler support and crashes app #6626
Comments
This example in a [Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
{
Console.WriteLine("First change exception!");
};
}
public override void OnBackPressed() => Task.Factory.StartNew(() => throw new Exception("foo"));
} However, if I do this, then public override void OnBackPressed() => throw new Exception("foo"); |
I think this behavior is correct for Debug builds ^^ The problem is Release builds (that are trimmed) crash with:
@steveisok do you have any ideas on which type I should check to see what is linked away? |
@steveisok @akoeplinger here is a smaller example: AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
{
Console.WriteLine("First chance exception!");
};
try
{
throw new Exception("foo");
}
catch
{
Console.WriteLine("Exception handled!");
} I tested with In a trimmed build (Release), this crashes without much info was to what is wrong:
So I was looking at: What would preserve the ctor of this type? I think ILSpy is showing this type has no ctor. |
The runtime is creating the exception so we need to preserve it. See dotnet/android#6626
Thanks, I opened dotnet/runtime#68235 to fix this. In the meantime you can workaround the issue by adding this to a custom linker configuration: <assembly fullname="System.Private.CoreLib">
<type fullname="System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs">
<method signature="System.Void .ctor(System.Exception)" />
</type>
</assembly> |
The runtime is creating the exception so we need to preserve it. See dotnet/android#6626
The runtime is creating the exception so we need to preserve it. See dotnet/android#6626 While looking at this I noticed that `FirstChanceExceptionEventArgs` is in ExceptionNotification.cs for some reason, fixed the filename to match the type name. Use DynamicDependency for preserving `FirstChanceExceptionEventArgs` and `UnhandledExceptionEventArgs`
The runtime is creating the exception so we need to preserve it. See dotnet/android#6626 While looking at this I noticed that `FirstChanceExceptionEventArgs` is in ExceptionNotification.cs for some reason, fixed the filename to match the type name. Use DynamicDependency for preserving `FirstChanceExceptionEventArgs` and `UnhandledExceptionEventArgs`
) Fixes: #6626 Context: dotnet/runtime#68235 If you create a .NET 6 app whichs ubscribes to the [`AppDomain.FirstChanceException`][0] event: partial class MainActivity { protected override void OnCreate (Bundle? savedInstanceState) { AppDomain.CurrentDomain.FirstChanceException += (o, e) => { Console.WriteLine ("First chance exception!"); }; } } and has a way to cause an exception to be thrown: partial class MainActivity { public override void OnBackPressed() => throw new Exception ("wee?"); } and is built in Release configuration with Trimming enabled, then the app will crash when the exception is thrown (in the above case, by launching the app and then pressing the Back button): E companyname.fo: * Assertion at /__w/1/s/src/mono/mono/metadata/object.c:4179, condition `ctor' not met The cause of the crash is that the linker removes the [`FirstChanceExceptionEventArgs(Exception)`][1] constructor. This was fixed in dotnet/runtime#68235 by using appropriate `[DynamicDependency]` attributes. However, this runtime fix is unlikely to be released before our next RC. Add a workaround by updating `src/Microsoft.Android.Sdk.ILLink/PreserveLists/System.Private.CoreLib.xml` so that the linker won't remove the `FirstChanceExceptionEventArgs(Exception)` constructor. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0 [1]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.firstchanceexceptioneventargs.-ctor?view=net-6.0
) Fixes: #6626 Context: dotnet/runtime#68235 If you create a .NET 6 app whichs ubscribes to the [`AppDomain.FirstChanceException`][0] event: partial class MainActivity { protected override void OnCreate (Bundle? savedInstanceState) { AppDomain.CurrentDomain.FirstChanceException += (o, e) => { Console.WriteLine ("First chance exception!"); }; } } and has a way to cause an exception to be thrown: partial class MainActivity { public override void OnBackPressed() => throw new Exception ("wee?"); } and is built in Release configuration with Trimming enabled, then the app will crash when the exception is thrown (in the above case, by launching the app and then pressing the Back button): E companyname.fo: * Assertion at /__w/1/s/src/mono/mono/metadata/object.c:4179, condition `ctor' not met The cause of the crash is that the linker removes the [`FirstChanceExceptionEventArgs(Exception)`][1] constructor. This was fixed in dotnet/runtime#68235 by using appropriate `[DynamicDependency]` attributes. However, this runtime fix is unlikely to be released before our next RC. Add a workaround by updating `src/Microsoft.Android.Sdk.ILLink/PreserveLists/System.Private.CoreLib.xml` so that the linker won't remove the `FirstChanceExceptionEventArgs(Exception)` constructor. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-6.0 [1]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.exceptionservices.firstchanceexceptioneventargs.-ctor?view=net-6.0
* Workaround FirstChanceExceptionEventArgs being trimmed See dotnet/android#6626 * Don't use ILLink descriptor on Microsoft.macOS.dll It is using CoreCLR which doesn't run into the issue.
) * [mono] Preserve FirstChanceExceptionEventArgs ctor The runtime is creating the exception so we need to preserve it. See dotnet/android#6626 * PR feedback: use DynamicDependencyAttribute instead Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
Android application type
Android for .NET (net6.0-android, etc.)
Affected platform version
.NET 6.0.100
Description
When registering an
AppDomain.CurrentDomain.FirstChanceException
handler, the handler works fine with non-trimmed builds.With the default
TrimMode=link
option when usingPublishTrimmed=true
, the app crashes at runtime (presumable when the handler gets called for the first time).I also tried the following, but the crash still happens at runtime:
I wanted to use a
FirstChanceException
handler to break into the code when an exception because of another trimmed method occurs, so that I could take a look at the call stack and the parameters of aSystem.Delegate.CreateDelegate(Type type, Type target, String method)
call, so that I could investigate which target method got trimmed in #6612 (comment).Unfortunately, since the
TrimMode=link
also trims away theFirstChanceException
handler support, I can't use it for that purpose.Steps to Reproduce
See description.
Did you find any workaround?
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: