问题
I'm working with an implementation of an MQTT client from the PAHO library in Android. Sometimes, the application locks up when it tried to publish a message (I think) and I can't figure out what's going on.
I also apologize in advance for the all to long post. I really don't know what the important bits are and the SO "spoiler" tag >!
wouldn't work to hide the details.
The MQTT client is working in a separate thread but I'm calling the mqtt client instance from the UI thread. Perhaps this causes a lockup? Should I instead define the client as an instance of the thread and call a method in the thread which forwards the action to the client?
When the app hangs, after a while ANR kicks in and asks to close the app. When I do that I get the following stack trace for my process from /data/anr/traces.txt:
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)
"main" prio=5 tid=1 WAIT
| group="main" sCount=1 dsCount=0 obj=0x40a59460 self=0x109d828
| sysTid=22050 nice=0 sched=0/0 cgrp=default handle=1074492552
| schedstat=( 8524633000 459501000 2204 ) utm=827 stm=25 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x410f4bd0> (a java.lang.Object)
at java.lang.Object.wait(Object.java:364)
at org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent(MqttDeliveryTokenImpl.java:120)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:85)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
at org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
at se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
at se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
at se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
at se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:89)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
"Thread-3645" prio=5 tid=12 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4111a5f8 self=0x13cc698
| sysTid=22083 nice=0 sched=0/0 cgrp=default handle=19370528
| schedstat=( 35731000 17350000 70 ) utm=1 stm=2 core=0
at dalvik.system.NativeStart.run(Native Method)
"MQTT Client Callback" prio=5 tid=17 WAIT
| group="main" sCount=1 dsCount=0 obj=0x41118bf0 self=0x13bb500
| sysTid=22082 nice=0 sched=0/0 cgrp=default handle=20691280
| schedstat=( 392860000 110613000 493 ) utm=35 stm=4 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x410d1c48> (a java.lang.Object)
at java.lang.Object.wait(Object.java:364)
at org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent(MqttDeliveryTokenImpl.java:120)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:85)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
at org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
at se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
at se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
at se.chalmers.pd.dashboard.ApplicationController.handleSystemMessage(ApplicationController.java:210)
at se.chalmers.pd.dashboard.ApplicationController.onMessage(ApplicationController.java:154)
at se.chalmers.pd.dashboard.MqttWorker$1$CustomMqttCallback.notifyCallbacks(MqttWorker.java:133)
at se.chalmers.pd.dashboard.MqttWorker$1$CustomMqttCallback.messageArrived(MqttWorker.java:128)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:248)
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:139)
at java.lang.Thread.run(Thread.java:856)
"MQTT Client Comms Sender" prio=5 tid=16 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x41118ae0 self=0x13bada0
| sysTid=22081 nice=0 sched=0/0 cgrp=default handle=20689392
| schedstat=( 6038000 11147000 189 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x410ffd90> (a java.lang.Object)
at java.lang.Object.wait(Object.java:401)
at org.eclipse.paho.client.mqttv3.internal.ClientState.get(ClientState.java:555)
at org.eclipse.paho.client.mqttv3.internal.CommsSender.run(CommsSender.java:83)
at java.lang.Thread.run(Thread.java:856)
"MQTT Client Comms Receiver" prio=5 tid=15 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x41116048 self=0x13ba870
| sysTid=22080 nice=0 sched=0/0 cgrp=default handle=20619056
| schedstat=( 656144000 184252000 2369 ) utm=59 stm=6 core=1
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:131)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:236)
at java.io.DataInputStream.readByte(DataInputStream.java:96)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:51)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:86)
at java.lang.Thread.run(Thread.java:856)
"CookieSyncManager" prio=5 tid=14 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410d0ad0 self=0x1286e20
| sysTid=22066 nice=10 sched=0/0 cgrp=bg_non_interactive handle=19445224
| schedstat=( 562000 10095000 5 ) utm=0 stm=0 core=0
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:118)
at android.os.Looper.loop(Looper.java:118)
at android.webkit.WebSyncManager.run(WebSyncManager.java:90)
at android.webkit.CookieSyncManager.run(CookieSyncManager.java:61)
at java.lang.Thread.run(Thread.java:856)
"AsyncTask #1" prio=5 tid=13 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410d0920 self=0x121f5b0
| sysTid=22065 nice=10 sched=0/0 cgrp=bg_non_interactive handle=19335920
| schedstat=( 5753000 108824000 12 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x410d0ab8> (a java.lang.VMThread) held by tid=13 (AsyncTask #1)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:157)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2022)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1009)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1069)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
"WebViewCoreThread" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410caa38 self=0x1266550
| sysTid=22063 nice=0 sched=0/0 cgrp=default handle=18808064
| schedstat=( 273871000 128721000 264 ) utm=20 stm=7 core=0
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:118)
at android.os.Looper.loop(Looper.java:118)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:728)
at java.lang.Thread.run(Thread.java:856)
"Binder Thread #2" prio=5 tid=10 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410c1ef0 self=0x129de58
| sysTid=22062 nice=0 sched=0/0 cgrp=default handle=19025912
| schedstat=( 1983000 23992000 8 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=9 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x410ba370 self=0x127cc90
| sysTid=22061 nice=0 sched=0/0 cgrp=default handle=18949528
| schedstat=( 2932000 32665000 18 ) utm=0 stm=0 core=0
at dalvik.system.NativeStart.run(Native Method)
"FinalizerWatchdogDaemon" daemon prio=5 tid=8 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b6320 self=0x1227790
| sysTid=22059 nice=0 sched=0/0 cgrp=default handle=19405328
| schedstat=( 3721000 6943000 27 ) utm=0 stm=0 core=0
at java.lang.VMThread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:1031)
at java.lang.Thread.sleep(Thread.java:1013)
at java.lang.Daemons$FinalizerWatchdogDaemon.run(Daemons.java:213)
at java.lang.Thread.run(Thread.java:856)
"FinalizerDaemon" daemon prio=5 tid=7 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b61c8 self=0x12207b8
| sysTid=22058 nice=0 sched=0/0 cgrp=default handle=19405208
| schedstat=( 1055000 601000 7 ) utm=0 stm=0 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x40a4f5d0> (a java.lang.ref.ReferenceQueue)
at java.lang.Object.wait(Object.java:401)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:102)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:73)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
at java.lang.Thread.run(Thread.java:856)
"ReferenceQueueDaemon" daemon prio=5 tid=6 WAIT
| group="main" sCount=1 dsCount=0 obj=0x410b6060 self=0x129d168
| sysTid=22057 nice=0 sched=0/0 cgrp=default handle=19404976
| schedstat=( 573000 395000 11 ) utm=0 stm=0 core=1
at java.lang.Object.wait(Native Method)
- waiting on <0x40a4f4f8>
at java.lang.Object.wait(Object.java:364)
at java.lang.Daemons$ReferenceQueueDaemon.run(Daemons.java:128)
at java.lang.Thread.run(Thread.java:856)
"Compiler" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5f70 self=0x127c4d0
| sysTid=22056 nice=0 sched=0/0 cgrp=default handle=19172728
| schedstat=( 151015000 59330000 862 ) utm=9 stm=6 core=0
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=4 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5e88 self=0x12b39c8
| sysTid=22055 nice=0 sched=0/0 cgrp=default handle=19323736
| schedstat=( 4263000 2800000 18 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x410b5d90 self=0x1268fd8
| sysTid=22054 nice=0 sched=0/0 cgrp=default handle=19014912
| schedstat=( 3760000 203000 4 ) utm=0 stm=0 core=1
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x410b5cb0 self=0x12257d8
| sysTid=22053 nice=0 sched=0/0 cgrp=default handle=19042944
| schedstat=( 271726000 42594000 230 ) utm=26 stm=1 core=1
at dalvik.system.NativeStart.run(Native Method)
----- end 22050 -----
I've turned on strict mode and I do get some traces where it warns about disk writes but none of them are particulary long. These are the strict mode traces:
> > 04-30 10:29:50.053: D/StrictMode(22229): StrictMode policy violation;
> > ~duration=35 ms: android.os.StrictMode$StrictModeDiskReadViolation:
> > policy=31 violation=2 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1089)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.open(BlockGuardOs.java:106) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.open(IoBridge.java:390) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.(FileOutputStream.java:88) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.io.FileOutputStream.(FileOutputStream.java:73) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:162)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=26 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.write(BlockGuardOs.java:190) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.write(IoBridge.java:447) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.write(FileOutputStream.java:187) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:163)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=18 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.write(BlockGuardOs.java:190) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.IoBridge.write(IoBridge.java:447) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > java.io.FileOutputStream.write(FileOutputStream.java:187) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:165)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method) 04-30 10:29:50.053:
>
>
> > D/StrictMode(22229): StrictMode policy violation; ~duration=2 ms:
> > android.os.StrictMode$StrictModeDiskWriteViolation: policy=31
> > violation=1 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1063)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > libcore.io.BlockGuardOs.fsync(BlockGuardOs.java:96) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.io.FileDescriptor.sync(FileDescriptor.java:71) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence.put(MqttDefaultFilePersistence.java:167)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientState.send(ClientState.java:397)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.internalSend(ClientComms.java:81)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:118)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > org.eclipse.paho.client.mqttv3.MqttTopic.publish(MqttTopic.java:62)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.ApplicationController.onLoadComplete(ApplicationController.java:358)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:95)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:275)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > android.os.Handler.dispatchMessage(Handler.java:99) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > android.os.Looper.loop(Looper.java:137) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > android.app.ActivityThread.main(ActivityThread.java:4424) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invokeNative(Native Method) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > java.lang.reflect.Method.invoke(Method.java:511) 04-30 10:29:50.053:
> > D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
> > 04-30 10:29:50.053: D/StrictMode(22229): at
> > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30
> > 10:29:50.053: D/StrictMode(22229): at
> > dalvik.system.NativeStart.main(Native Method)
Edit: this is how I enable StrictMode
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
Edit 2: for future reference and to add the cause of the issue I figured I should put something in here for the search engines to find.
It turned out the problem was with the Node.JS based broker mqttjs we had. It could just be our implementation, I haven't tested the example from the mqttjs developer. The broker stopped publishing and returning tokens after a certain number of messages had been published. The MQTT client was running in another thread but since it was called from the UI thread it caused some issues.
Anyway, we changed to Mosquitto and now everything is working well.
回答1:
Your ANR happens in
org.eclipse.paho.client.mqttv3.internal.MqttDeliveryTokenImpl.waitUntilSent
because this method calls Object.wait
, probably waiting for another thread to notify it.
Your call is apparently here
se.chalmers.pd.dashboard.MqttWorker.publish(MqttWorker.java:177)
which is called in your implementation of the WebClient
se.chalmers.pd.dashboard.MainActivity$CustomWebViewClient.onPageFinished(MainActivity.java:89)
This onPageFinished
method is called on the UI thread. Calling MqttTopic.publish
is a network call and apparently blocks until the call is done. You should put that in a Thread or an AsyncTask so it does not lock the UI Thread.
回答2:
You're trying to do long running network comms on the UI thread. This blocks the UI from updating, which is bad. Android detects this fact and handles it as an ANR.
I don't know what bits are your code, but somewhere around here:
se.chalmers.pd.dashboard.ApplicationController.sendResponse(ApplicationController.java:238)
you should put the MQTT work in a new thread. If you then need to update the UI as a result of the MQTT work completing, use the Handler.
回答3:
As you've enabled the strict mode, you are not allowed to access disk or network while in the UI Thread.
Reading your stacktrace, it's obvious that a call to MqttWorker.publish leads to a disk access. You should put this call in an AsyncTask, or another thread than the UI.
来源:https://stackoverflow.com/questions/16295862/interpreting-an-anr-stack-trace