analytics-android icon indicating copy to clipboard operation
analytics-android copied to clipboard

Segment 4.7.1IndexOutOfBoundsException: Index: 0, Size: 0

Open dmytrodanylyk opened this issue 4 years ago • 5 comments

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.

dmytrodanylyk avatar Sep 11 '20 03:09 dmytrodanylyk

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);

dmytrodanylyk avatar Sep 11 '20 07:09 dmytrodanylyk

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.

artour-bakiev avatar Sep 15 '20 03:09 artour-bakiev

Hi all! we have created an internal ticket to track work on this :). Will post here if we get any updates.

prayansh avatar Nov 10 '20 00:11 prayansh

was this fixed on segment lib side?

kozyrevsergey89 avatar Apr 22 '21 02:04 kozyrevsergey89

Not yet

prayansh avatar Apr 23 '21 00:04 prayansh

close with #824 in release 4.11.1

wenxi-zeng avatar Mar 14 '23 16:03 wenxi-zeng