analytics-android
analytics-android copied to clipboard
Segment 4.7.1IndexOutOfBoundsException: Index: 0, Size: 0
Some of our users experience following crash:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at androidx.lifecycle.LifecycleRegistry.calculateTargetState(LifecycleRegistry.java:162)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:183)
at com.segment.analytics.Analytics.<init>(Analytics.java:323)
at com.segment.analytics.Analytics$Builder.build(Analytics.java:1395)
at com.atlassian.mobilekit.module.analytics.atlassian.segment.SegmentHub.<init>(SegmentHub.kt:21)
at
....
This crash is happening inside the public Analytics build()
method in the return new Analytics(...)
.
Because of the above crash, we face other issues, we can't recreate the Analytics
object because previous crash added a tag to the instances map:
synchronized (INSTANCES) {
if (INSTANCES.contains(tag)) {
throw new IllegalStateException(
"Duplicate analytics client created with tag: "
+ tag
+ ". If you want to use multiple Analytics clients, use a different writeKey "
+ "or set a tag via the builder during construction.");
}
INSTANCES.add(tag);
}
Please revisit the idea of adding the tag to the instances map before Analytics
object is created.
After digging a bit deeper we found that the problem is because we create Analytics
on the background thread which causes internal race-condition in the ProcessLifecycleOwner.get().getLifecycle()
which is used in the Analytics
.
The fix on our side can be simple - create Analytics
object on the UI thread.
The fix on the segment library side - switch to UI thread when executing lifecycle.addObserver(activityLifecycleCallback);
and lifecycle.removeObserver(activityLifecycleCallback);
Has been introduced by https://github.com/segmentio/analytics-android/commit/37226c116c5491e3fec2d27800e1ccc297da9de4 which broke an implicit contract that Analytics
class can be created in a background thread.
Hi all! we have created an internal ticket to track work on this :). Will post here if we get any updates.
was this fixed on segment lib side?
Not yet
close with #824 in release 4.11.1