3

I am a developer of an application. Recently I found via the ANR reports from the play store that a considerable chunk of my user base is facing ANR issue. When I tried to get under the skin of the issue, I found out that the reason of these ANRs is Google Tag Manager (GTM). I am using GTM to push analytics events to Google Analytics console, to be further used for for customer behaviour analysis. The ANR is being caused by GTM is because of a DEADLOCK SITUATION. This deadlock is being caused as GTM is blocking the execution of the main thread, resulting in a circular wait, causing to freeze to screen at the same point for a time period exceeding 5 secs, triggering an ANR. The excerpt from the thread dump illustrating the above issue is pasted below:

"main" prio=5 tid=1 Blocked 
  | group="main" sCount=1 dsCount=0 obj=0x72b04f90 self=0xb5007800 
  | sysTid=14478 nice=0 cgrp=apps sched=0/0 handle=0xb6f59ec8 
  | state=S schedstat=( 9138649658 3369477995 8161 ) utm=867 stm=46 core=0 HZ=100 
  | stack=0xbe1e8000-0xbe1ea000 stackSize=8MB 
  | held mutexes= 
  at com.google.android.gms.tagmanager.o.w(unavailable:-1) 
  - waiting to lock <0x3f235d4c> (a com.google.android.gms.tagmanager.o) held by thread 20 
  at com.google.android.gms.tagmanager.o.a(unavailable:-1) 
  at com.google.android.gms.tagmanager.o$d.nU(unavailable:-1) 
  at com.google.android.gms.tagmanager.n.refresh(unavailable:-1) 
  - locked <0x27618dcf> (a com.google.android.gms.tagmanager.n) 
 ...

--- 

"pool-4-thread-1" prio=5 tid=20 Blocked 
  | group="main" sCount=1 dsCount=0 obj=0x32c08560 self=0xafa39400 
  | sysTid=14511 nice=0 cgrp=apps sched=0/0 handle=0xafa4d080 
  | state=S schedstat=( 5595762754 494490524 1998 ) utm=556 stm=3 core=1 HZ=100 
  | stack=0xa26f6000-0xa26f8000 stackSize=1036KB 
  | held mutexes= 
  at com.google.android.gms.tagmanager.n.a(unavailable:-1) 
  - waiting to lock <0x27618dcf> (a com.google.android.gms.tagmanager.n) held by thread 1 
  at com.google.android.gms.tagmanager.o.a(unavailable:-1) 
  - locked <0x3f235d4c> (a com.google.android.gms.tagmanager.o) 
  at com.google.android.gms.tagmanager.o.a(unavailable:-1) 
  at com.google.android.gms.tagmanager.o$c.b(unavailable:-1) 
  - locked <0x3f235d4c> (a com.google.android.gms.tagmanager.o) 
  at com.google.android.gms.tagmanager.o$c.l(unavailable:-1) 
  at com.google.android.gms.tagmanager.co.oL(unavailable:-1) 
  at com.google.android.gms.tagmanager.co.run(unavailable:-1) 
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 

Code Snippet for GTM :

private void loadGTMContainer() {
        mAnalyticsService.loadGTMContainer(new GenericCallBack<ContainerHolder>() {

            @Override
            public void onSuccess(ContainerHolder result) {
                if (result != null) {
                    LOGGER.debug("Google Tag Manager container loaded successfully");
                    //manually refresh the container, just in case
                    result.refresh();
                    //...
                }
            }

            @Override
            public void onError(Exception e) {
                //...
            }
        });
    }

While debugging the issue, I found two links, mentioned below;

Link 1 Link 2

These directed that in order to resolve this deadlock, the way is to remove result.refresh(), which will remove the deadlock.

This strategy has helped me to tackle the issue. The frequency of ANRs has reduced considerably, but yet the issue is still present as the users, though a very less number, are facing the same issue. The thread dumps from latest ANRs also indicate the same problem of deadlock.

So, Is there a peormanent solution or a fix to this issue, or the removal of GTM from the app is the only wayout. Can someone suggest me some other alternative libraries in place of GTM, which can be used to communicate with Google Analytics to push the anlytics events?

FiXiT
  • 769
  • 1
  • 12
  • 27

0 Answers0