title: Android MONKEY ANR at android.os.MessageQueue.nativePollOnce
body: <p>We find a tricky ANR problem when using MONKEY to test our app.</p>
  
  <p>CPU and memory using is in the normal level.</p>
  
  <p>it seems that the ANR is cause by a Timeout KeyEvent faked by MONKEY sending to our app’s Activity.
  Below is the part of log:</p>
  
  <pre><code>“
  ANR:
      Time: 2016-12-29 15:36:08
      Window: AppWindowToken{56a48d3 token=Token{abb12c2 ActivityRecord{cb0230d u0 &lt;our app’s activity&gt; t34}}} - Window{40c381a u0 &lt;our app’s activity&gt;}
      DispatchLatency: 5005.0ms
      WaitDuration: 5003.6ms
      Reason: Waiting to send key event because the focused window has not finished processing all of the input events that were previously delivered to it.  Outbound queue length: 0.  Wait queue length: 1.
  
   ANR:
  11: channelName='40c381a &lt;our app’s activity&gt; (server)', windowName='Window{40c381a u0 &lt;our app’s activity&gt;', status=NORMAL, monitor=false, inputPublisherBlocked=false
        OutboundQueue: &lt;empty&gt;
        WaitQueue: length=1
          KeyEvent(deviceId=-1, source=0x00000101, action=0, flags=0x00000008, keyCode=82, scanCode=0, metaState=0x00000000, repeatCount=0), policyFlags=0x6b000000, eventT=12621294000000, targetFlags=0x00000101, resolvedAction=0, age=5014.4ms, wait=5011.8ms
  ”
  
  But the trace log is
  “
  "main" prio=5 tid=1 Native
    | group="main" sCount=1 dsCount=0 obj=0x75fc23f0 self=0x7fa2f04a00
    | sysTid=1478 nice=0 cgrp=default sched=0/0 handle=0x7fa321ba98
    | state=S schedstat=( 0 0 0 ) utm=3806 stm=1660 core=0 HZ=100
    | stack=0x7fca45d000-0x7fca45f000 stackSize=8MB
    | held mutexes=
    kernel: __switch_to+0x7c/0x88
    kernel: SyS_epoll_wait+0x298/0x328
    kernel: SyS_epoll_pwait+0xa4/0x118
    kernel: el0_svc_naked+0x24/0x28
    native: #00 pc 000000000006b418  /system/lib64/libc.so (__epoll_pwait+8)
    native: #01 pc 000000000001e7cc  /system/lib64/libc.so (epoll_pwait+64)
    native: #02 pc 000000000001a26c  /system/lib64/libutils.so (_ZN7android6Looper9pollInnerEi+156)
    native: #03 pc 000000000001a120  /system/lib64/libutils.so (_ZN7android6Looper8pollOnceEiPiS1_PPv+60)
    native: #04 pc 00000000000ef3bc  /system/lib64/libandroid_runtime.so (_ZN7android18NativeMessageQueue8pollOnceEP7_JNIEnvP8_jobjecti+48)
    native: #05 pc 00000000008e8000  /system/framework/arm64/boot-framework.oat (Java_android_os_MessageQueue_nativePollOnce__JI+140)
    at android.os.MessageQueue.nativePollOnce(Native method)
    at android.os.MessageQueue.next(MessageQueue.java:323)
    at android.os.Looper.loop(Looper.java:145)
    at com.android.server.SystemServer.run(SystemServer.java:419)
    at com.android.server.SystemServer.main(SystemServer.java:239)
    at java.lang.reflect.Method.invoke!(Native method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:938)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
  ”
  </code></pre>
  
  <p>Our main thread is waiting for get the message from MessageQueue.
  The inputmanager send a KeyEvent to our Activity but our main thread seems not receive the event and still waiting for the message to handle.</p>
  
  <p>What reason causes this problem?</p>