getsentry / sentry-dotnet

Sentry SDK for .NET
https://docs.sentry.io/platforms/dotnet
MIT License
578 stars 206 forks source link

[Android] FormatException when reading "processor_frequency" #3540

Closed TimBurik closed 2 weeks ago

TimBurik commented 3 weeks ago

Package

Sentry

.NET Flavor

.NET

.NET Version

8.0.303

OS

Android

SDK Version

4.10.0

Self-Hosted Sentry Version

No response

Steps to Reproduce

  1. Create an Android app and install Sentry NuGet package
  2. Configure Sentry so that EnableBeforeSend is enabled and BeforeSend callback is set
  3. Crash the app using SentrySdk.CauseCrash(CrashType.JavaBackgroundThread)

Expected Result

BeforeSend callback is fired, the java.lang.RuntimeException: This exception was caused deliberately by SentrySdk.CauseCrash(CrashType.JavaBackgroundThread) crash is reported.

Actual Result

The following crash is reported:

System.FormatException: One of the identified items was in an invalid format.
  File "ThrowHelper.cs", line 594, in void ThrowHelper.ThrowFormatException()
    throw new FormatException { Source = ExceptionSourceValueToRethrowAsJsonException };
  File "JsonElement.cs", line 660, in int JsonElement.GetInt32()
    ThrowHelper.ThrowFormatException();
  File "Device.cs", line 459, in Device Device.FromJson(JsonElement json)
    var processorFrequency = json.GetPropertyOrNull("processor_frequency")?.GetInt32();
  File "SentryContexts.cs", line 148, in SentryContexts SentryContexts.FromJson(JsonElement json)
    result[name] = Device.FromJson(value);
  File "MiscExtensions.cs", line 5, in SentryContexts MiscExtensions.Pipe<JsonElement, SentryContexts>(JsonElement input, Func<JsonElement, SentryContexts> pipe)
    public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> pipe) => pipe(input);
  File "SentryEvent.cs", line 305, in SentryEvent SentryEvent.FromJson(JsonElement json, Exception exception)
    var contexts = json.GetPropertyOrNull("contexts")?.Pipe(SentryContexts.FromJson);
  File "SentryEventExtensions.cs", line 29, in SentryEvent SentryEventExtensions.ToSentryEvent(SentryEvent sentryEvent, SentryOptions javaOptions)
    return SentryEvent.FromJson(json.RootElement, sentryEvent.Throwable);
  File "BeforeSendCallback.cs", line 26, in SentryEvent BeforeSendCallback.Execute(SentryEvent e, Hint h)
    var evnt = e.ToSentryEvent(_javaOptions);
  ?, in IntPtr IBeforeSendCallbackInvoker.n_Execute_Lio_sentry_SentryEvent_Lio_sentry_Hint_(IntPtr jnienv, IntPtr native__this, IntPtr native_p0, IntPtr native_p1)
  File "/Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs", line 231, col 5, in IntPtr JNINativeWrapper.Wrap_JniMarshal_PPLL_L(_JniMarshal_PPLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1)

This issue seems to be similar to #3465, the root cause seems to be a type mismatch for "processor_frequency" field:

bitsandfoxes commented 3 weeks ago

Thanks @TimBurik for doing all the sleuthing already! A fix is coming