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

Segment.Analytics.Mediator - Exception when initialising Analytics on iOS #97

Closed
IainS1986 opened this issue Mar 13, 2024 · 16 comments
Closed
Assignees

Comments

@IainS1986
Copy link

IainS1986 commented Mar 13, 2024

Describe the bug
I've just seen a consistent exception on my device (we've been using Segment for some time in Xamarin Native, have ported over to .net8, been running for a while but this is a random edge case it seems as its a new crash i've not seen before).

Just basic init below...

var configuration = new Configuration(SegmentKey,
                flushAt: 20,
                flushInterval: 30);
_analytics = new Analytics(configuration);

Theres an exception thrown,

System.AggregateException
2024-03-13 10:28:04.755 xxx.iOS[12074:9672560] A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Object reference not set to an instance of an object.)

---- Inner Exception
System.NullReferenceException
Object reference not set to an instance of an object.
   at Segment.Analytics.Mediator.<>c__DisplayClass1_0.<<Add>b__0>d.MoveNext()

Platform (please complete the following information):

  • Library Version in use: Segment.Analytics.CSharp/2.3.1
  • Platform being tested: iOS on .net8
@IainS1986 IainS1986 changed the title Exception when initialising Analytics on iOS Segment.Analytics.Mediator - Exception when initialising Analytics on iOS Mar 13, 2024
@IainS1986
Copy link
Author

It appears like everything still works fine, but the exception is being uncaught in a Task I believe inside Segement, which means I can't catch it and it fires off into our global error/exception handling for uncaught exceptions which is a pain

@IainS1986
Copy link
Author

It also seems to trigger as an uncaught exception twice

@wenxi-zeng
Copy link
Contributor

hi @IainS1986 thanks for reporting this issue. could you first try the following to help us better understand the problem:

  1. update the SDK to the latest version, see if the issue still persists
  2. follow this example to log the exception. if it's an AggregateException, log the inner exceptions as well

@nmaricmoberg
Copy link

nmaricmoberg commented Mar 18, 2024

@wenxi-zeng I can confirm having the same issue on 2.3.3

Sentry logs it as UnobservedTaskException.

iOS ::: System.NullReferenceException --- async Task <>c__DisplayClass1_0.<Add>b__0(?)+(?) => { }
Android ::: System.NullReferenceException --- async void Mediator.Add(Plugin plugin)+(?) => { }

Assembly: Segment.Analytics.CSharp
Version: 2.3.3.0

Will update to 2.3.4 and report back.

@wenxi-zeng
Copy link
Contributor

@nmaricmoberg thanks for confirming this issue and providing the logs. the android log is very helpful. I do believe we have a potential null pointer exception in this line. we'll get it fixed asap.

@MichaelGHSeg
Copy link
Contributor

From a bit of investigation, I thought we were already checking the settings and I wouldn't expect any other references to be null at that point. I'll keep looking. @nmaricmoberg, could you provide more of the Segment logs?

@nmaricmoberg
Copy link

nmaricmoberg commented Mar 19, 2024

@wenxi-zeng @MichaelGHSeg Hey there. Thanks for taking a look into this.
I have added the exception handler in the Analytics ctor. Also updated to version 2.3.4.0

I am using .NET Native for Android and iOS, i.e. my mobile projects are targeting net8.0-android and net8.0-ios respectively.
Alongside the previous error I also get this:

System.TypeLoadException
Could not load type of field 'Segment.Analytics.Analytics+<Update>d__71:<>t__builder' (1) due to: Could not resolve type with token 01000031 from typeref (expected class 'System.Runtime.CompilerServices.AsyncVoidMethodBuilder' in assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51') assembly:netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 type:System.Runtime.CompilerServices.AsyncVoidMethodBuilder member:(null)

Also unrelated but, AOT compilation is not possible and throws an error regarding Segment.
This warning jumps out regardless of AOT or not (occured in all previous versions as well):

2>resource Segment.Analytics.CSharp.xml in Segment.Analytics.CSharp, Version=2.3.4.0, Culture=neutral, PublicKeyToken=null(8,10): warning IL2008: Segment.Analytics.CSharp: Could not resolve type 'System.Linq.Expressions.Interpreter.LightLambda'.
2>resource Segment.Analytics.CSharp.xml in Segment.Analytics.CSharp, Version=2.3.4.0, Culture=neutral, PublicKeyToken=null(8,10): warning IL2008: Segment.Analytics.CSharp: Could not resolve type 'System.Linq.Expressions.Interpreter.LightLambda'.
2>resource Segment.Analytics.CSharp.xml in Segment.Analytics.CSharp, Version=2.3.4.0, Culture=neutral, PublicKeyToken=null(8,10): warning IL2008: Segment.Analytics.CSharp: Could not resolve type 'System.Linq.Expressions.Interpreter.LightLambda'.
2>resource Segment.Analytics.CSharp.xml in Segment.Analytics.CSharp, Version=2.3.4.0, Culture=neutral, PublicKeyToken=null(8,10): warning IL2008: Segment.Analytics.CSharp: Could not resolve type 'System.Linq.Expressions.Interpreter.LightLambda'.

@nmaricmoberg
Copy link

nmaricmoberg commented Mar 19, 2024

I have managed to mitigate the System.TypeLoadException error by creating an extra project XYZ.Common that targets netstandard2.1. The .NET 8 target causes that issue. I only reference Segment Analytics CSharp 2.3.4 in that project and I hold a static Analytics instance there that both projects (iOS and Android) use.

I believe you need to directly add support for .NET 5-6-7-8 in the package.

The build warnings are still there, and the original issue that Sentry reported is still there:

System.NullReferenceException
Object reference not set to an instance of an object
async void Mediator.Add(Plugin plugin)+(?) => { }

Also, you could try adding Xamarin.Native sample apps that target .NET 8. That could give you some further insight.

@wenxi-zeng
Copy link
Contributor

@nmaricmoberg thanks for this detailed logs and diagnosing info. it's super. we'll take a look. in the meanwhile, please keep using the workaround you proposed. sorry for the inconvenience it may cause.

dimitri-dev added a commit to dimitri-dev/Analytics-CSharp that referenced this issue Apr 7, 2024
@alimakki
Copy link

Getting a similar error as this, however in Unity, limited stacktrace captured from sentry.

I used the UnitySample patterns in our code to configure and Segment.

System.NullReferenceException
 
Segment.Analytics.Mediator+<>c__DisplayClass1_0+<<Add>b__0

Using the following version of Segment 2.3.4.0

@dimitri-dev
Copy link
Contributor

@alimakki Try building the fix for #98 locally and running with that. Wish it was an actual confirmed fix and released but the maintainers are a little bit busy I guess?

@IainS1986
Copy link
Author

Just an FYI in case people weren't aware.

I'm getting this on Android too.

@MiBu
Copy link

MiBu commented Apr 18, 2024

@wenxi-zeng we are also seeing this issue and trying the fix from #98 fixes it, but we are not comfortable deploying without you taking a look and approving it.

@wenxi-zeng
Copy link
Contributor

sorry for the delayed response. the fix from #98 looks promising. we will do some tests on our end to make sure everything works. will ship a release soon.

MichaelGHSeg pushed a commit that referenced this issue Apr 22, 2024
@wenxi-zeng
Copy link
Contributor

wenxi-zeng commented Apr 22, 2024

hi all, the latest version 2.3.5 is released on both nuget and openupm with the fix @dimitri-dev proposed. we have verified the issue is resolved on our test devices. thanks!

@alimakki
Copy link

Thank you @wenxi-zeng @dimitri-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants