Skip to content
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

System.MethodAccessException MAUI app startup crash #3975

Open
plequere-ms opened this issue Feb 17, 2025 · 15 comments
Open

System.MethodAccessException MAUI app startup crash #3975

plequere-ms opened this issue Feb 17, 2025 · 15 comments

Comments

@plequere-ms
Copy link

Package

Sentry

.NET Flavor

.NET

.NET Version

9.0.0

OS

Android

SDK Version

5.1.1

Self-Hosted Sentry Version

No response

Steps to Reproduce

Since upgrading Sentry.Maui from 5.1.0 to 5.1.1, I am occasionally seeing the following crash:

System.MethodAccessException: Method Android.Runtime.AndroidEnvironmentInternal.UnhandledException(System.Exception)' is inaccessible from method Sentry.JavaSdk.SentryOptions+IBeforeSendCallbackInvoker.n_Execute_Lio_sentry_SentryEvent_Lio_sentry_Hint__mm_wrapper(intptr,intptr,intptr,intptr)'

Unfortunately, I don't have a good repro. This only happens occasionally. Empirical evidence seems to suggest that this might have something to do with network conditions, but that's merely a guess.

My app builds with the following settings, which might be relevant:

    <!-- Trim settings -->
    <TrimMode>partial</TrimMode>
    <IsTrimmable>true</IsTrimmable>
    <TrimOptimization>release</TrimOptimization>

Please let me know if I can provide any additional information.

Expected Result

No crash :)

Actual Result

App crashes immediately upon startup.

https://sound-imagineers-llc.sentry.io/issues/6306102051/?project=4503996749643777&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=issue-stream&statsPeriod=90d&stream_index=0

@jamescrosswell
Copy link
Collaborator

Hm, very curious. We did add some code to wire up an internal before send event handler in 5.1.1 (in the SentrySdk class):

But that new code doesn't refer to Android.Runtime.AndroidEnvironmentInternal.UnhandledException anywhere. The error you're seeing indicates that some code is trying to reference Android.Runtime.AndroidEnvironmentInternal.UnhandledException, but this isn't permitted:

The exception that is thrown when there is an invalid attempt to access a method, such as accessing a private method from partially trusted code.

Do you have a custom BeforeSend event handler configured? Is it possible your own code is trying to access that member?

@plequere-ms
Copy link
Author

I do not have a BeforeSend handler, and I can't think of anything in my code that would try to access that member. My initialization code is as follows:

            .UseSentry(options =>
            {
                options.Dsn = "https://XXXXXXXXXXXX";
                options.TracesSampleRate = 1.0;
                options.IsGlobalModeEnabled = true;
                options.IncludeTitleInBreadcrumbs = true;
                options.IncludeTextInBreadcrumbs = true;
                options.IncludeBackgroundingStateInBreadcrumbs = true;
                options.StackTraceMode = StackTraceMode.Enhanced;
                options.AttachScreenshot = true;
            })

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 18, 2025
@jamescrosswell
Copy link
Collaborator

Thanks @plequere-ms. I can't immediately see how/where this might be happening and it's difficult to investigate without being able to reproduce it.

If anyone can figure out how to reproduce this then we can look into it further.

@bcaceiro
Copy link

Not sure if related, but after upgrading to latest Version - I also have a crash at startup in Android + iOS - it says the DSN is not configured ( which it is, we didnt change anything) - after reverting to previous version 5.1.0 it seems to be okay

Will later open a new issue if necessary

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 18, 2025
@jamescrosswell
Copy link
Collaborator

Not sure if related, but after upgrading to latest Version - I also have a crash at startup in Android + iOS - it says the DSN is not configured ( which it is, we didnt change anything) - after reverting to previous version 5.1.0 it seems to be okay

Will later open a new issue if necessary

Not related no... but also inexplicable. There's nothing in the 5.1.1 release that should cause that.

Are you able to open a new issue with a small sample app that demonstrates the issue you're describing?

@bcaceiro
Copy link

Not sure if related, but after upgrading to latest Version - I also have a crash at startup in Android + iOS - it says the DSN is not configured ( which it is, we didnt change anything) - after reverting to previous version 5.1.0 it seems to be okay
Will later open a new issue if necessary

Not related no... but also inexplicable. There's nothing in the 5.1.1 release that should cause that.

Are you able to open a new issue with a small sample app that demonstrates the issue you're describing?

Yes! I'll try by end of the week - Still have to do more testing in my app, and then I can try a small repo + some crash logs to be sure

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 18, 2025
@jamescrosswell
Copy link
Collaborator

Thanks @bcaceiro !

For this System.MethodAccessException issue then, so that it doesn't get lost, we still need a way to reproduce this.

@aritchie
Copy link
Collaborator

@plequere-ms @bcaceiro This looks to be a case of the compiler trimming things it shouldn't. My suggestion is to look at: https://learn.microsoft.com/en-us/dotnet/maui/deployment/trimming?view=net-maui-9.0 - specifically: "Preserve assemblies, types, and members" section.

For your MAUI projects, I would suggest adding the following

csproj

<ItemGroup>
  <TrimmerRootDescriptor Include="Linker.xml" />
</ItemGroup>

The XML file

<?xml version="1.0" encoding="UTF-8" ?>
<linker>
    <assembly fullname="Sentry" preserve="all" />
    <assembly fullname="Sentry.Bindings.Android" preserve="all" />
</linker>

@dartasen
Copy link

@aritchie Can still report this exact same issue, with trimming being disabled...

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 26, 2025
@aritchie
Copy link
Collaborator

@dartasen thank you for the update. Did you try the linker configuration though? That isn't disabling the trimmer really. Can you provide any sort of sample we can look at?

@obriejas
Copy link

obriejas commented Mar 3, 2025

We have been getting these at a very high consistency as well. I tried implementing those linker configurations, but it had no effect sadly. We are also not using any sort of CustomSend and are using these options:

options.Debug = false;
options.Environment = "appstore";
options.SampleRate = 1.0f;
options.ShutdownTimeout = TimeSpan.FromSeconds(15);
options.TracesSampleRate = 0.20D;

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Mar 3, 2025
@aritchie
Copy link
Collaborator

aritchie commented Mar 3, 2025

@obriejas We haven't been able to reproduce this on our end. Are you able to provide a reproducible sample?

@tranb3r
Copy link

tranb3r commented Mar 13, 2025

I'm also having this MethodAccessException, and app crashing on startup, with version 5.2.0.
The only solution is to clear the app cache.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Mar 13, 2025
@bitsandfoxes
Copy link
Contributor

I think we're still kind of stuck without a minimal repro to step through this locally. Any help would be very appreciated.

@bruno-garcia
Copy link
Member

I wonder if we need to stop binding the native event to C#'s BeforeSend.
This might be a .NET or MAUI bug but ultimately our users get a crash (that we capture) and they get frustrated. So if bypassing the binding will stop this from happening, it's worth reducing the SDK's capabilities in favor of more stability.

That would mean native events don't have a BeforeSend unless we give them ways to write those in Swift/Kotlin, which could be considered at a later point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Status: No status
Development

No branches or pull requests

9 participants