1

my App runs fine on Android 4.4 - 2.3. but I recently updated my Nexus 7 to Android 5.0.2 (Lollipop) and only here I get an Error / ANR. But I don't know why. I can't find anything related here or in the web.

The ANR appears, when I start an AsyncTask. It runs fine first and after 5 seconds, the UI Thread freezes and I get alot of ART Runtime Errors. Below I listed some ErrorLines which keep repeating:

02-19 10:12:23.060: A/art(7229): art/runtime/thread_list.cc:170] Thread suspend timeout
02-19 10:12:23.060: A/art(7229): art/runtime/thread_list.cc:170] DALVIK THREADS (27): 
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170] "ReferenceQueueDaemon" daemon prio=5 tid=7 Waiting
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   | group="system" sCount=1 dsCount=0 obj=0x12c26080 self=0xac59c000
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   | sysTid=7240 nice=0 cgrp=apps sched=0/0 handle=0xac598380
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   | state=S schedstat=( 0 0 0 ) utm=0 stm=2 core=3 HZ=100
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   | stack=0xb421f000-0xb4221000 stackSize=1036KB
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   | held mutexes=
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   at java.lang.Object.wait!(Native method)
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   - waiting on <0x063dedd6> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:133)
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   - locked <0x063dedd6> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
02-19 10:12:23.061: A/art(7229): art/runtime/thread_list.cc:170]   at java.lang.Thread.run(Thread.java:818)

Same version of the app runs fine on all other APIs. Does Lollipop handles multitasking and wait() calls differently?

I hope anyone of you can help and maybe had a similar issue.

Thank you!

EDIT 1: Structure:

1: Click on UpdateButton starts AsyncTask which sends update requests to an Bluetooth device and waits for any incomming messages

2: Incomming Messages are stored in an MessageQueue (if this Queue is empty I call wait())

try {
    synchronized (myMessages) {
         myMessages.wait();
    }
} catch (InterruptedException ex) {
    return null;
}

3: if myMessages is not empty -> it sends an Update Intent to an BroadcastReceiver which updates a ListView

This repeats until the user clicks again on the UpdateButton. It works for about 5 seconds, then it freezes. There is no UI handling in the AsyncTask itself.

EDIT 2:

While debugging I get this really strange behaviour :D. I'll have my AsyncTask like described above: It's doInBackground Method looks like this:

@Override
protected String doInBackground(Void... params) {
     while(buttonClicked){
          // #POS: HERE IS THE STRANGE BEHAVIOR HAPPENING
          if(checkAnCondition){
                 doWork();
          }
     }
}

If I have the AsyncTask run in its while loop and there is nothing at #POS it throws the Application not Responding. During debugging I added a System.out.println("") Statement at this #POS and then.. The App runs fine :D WTF? :D

So.. Why does the app crashes if the first thing in the while statement is an if condition? Any Ideas?

Wicked161089
  • 481
  • 4
  • 18
  • Could you post your code to see if this gives some light?= – Carlos Verdes Feb 19 '15 at 09:40
  • I don't know where exactly this error occurs and it is a lot of code. I post the structure of the code in my next edit, hope that helps. – Wicked161089 Feb 19 '15 at 09:46
  • 1
    Then try to create an AsyncTask in a separate app just to reproduce the error or add logs to see which block of code is being freezed. – Carlos Verdes Feb 19 '15 at 09:56
  • Is there any code in your Activity (main thread) which is trying to access to myMessages variable? I understand that "myMessages" is a private var inside your AsyncTask BUT, if you are trying to update your ListView using directly "myMessages" var then... you are having a race condition. Please add all the places where myMessages is being used in your code. – Carlos Verdes Feb 19 '15 at 09:59

2 Answers2

1

The problem is that your are freezing a thread, it's not main thread so you have more "gap" before the ARN, but after 5 seconds... Android throws the error.

You could try to avoid blocking forever the thread checking each 3 seconds the queue (using timeout in your wait sentence), something like:

try {
    while( i have no messages){
        synchronized (myMessages) {
             myMessages.wait(300);
         }
    }
} catch (InterruptedException ex) {
return null;
}

I can't reproduce your error so... just let me know if this helps.

EDIT

I changed the while and timeout condition.

Carlos Verdes
  • 3,037
  • 22
  • 20
  • Debug is a bad way to reproduce a race condition, is your code working fine with System.out and no debug? – Carlos Verdes Feb 19 '15 at 10:37
  • If I have the System.out at the #POS it works fine.. if I delete it.. it crashes – Wicked161089 Feb 19 '15 at 10:43
  • It could be that System.out is adding a delay so your race condition is not being reproduced... I mean, when System.out is being executed a new message arrives and then your code never enters in the "wait" sentence. Try my answer and let me know if it helps. – Carlos Verdes Feb 19 '15 at 10:46
  • Try with 300 milliseconds instead of 3000 and the synch block inside the while statement (I've edited my answer) – Carlos Verdes Feb 19 '15 at 11:23
0

Issue is on condition statement in the while loop.use more valid statement for this.

e.g queue is empty

ayesh don
  • 1,121
  • 1
  • 15
  • 25