paho.mqtt.android icon indicating copy to clipboard operation
paho.mqtt.android copied to clipboard

Sometime I can't stop service run MqttAndroidClient

Open nguyenlinhnttu opened this issue 8 years ago • 6 comments

Please fill out the form below before submitting, thank you!

  • [v] Bug exists Release Version 1.1.1 (Java Repository Master Branch)

Description of Bug:

I use Service run MqttAndroidClient and use flag START_STICKY. When I logout account, i will stop my service. stopService(new Intent(getActivity(), ABCService.class)); But Sometime ABCService can't stop, MqttService just running. I don't know reason.

Console Log output (if available):

nguyenlinhnttu avatar Aug 19 '17 12:08 nguyenlinhnttu

Do you have any logs collected when you try to stop the service? It would suggest that something is causing the client to hang and so the service can't stop. Has the connection been closed first?

jpwsutton avatar Aug 25 '17 12:08 jpwsutton

Please check code MySevice.java

public void onDestroy() { super.onDestroy(); try { if (mqttAndroidClient != null) { mqttAndroidClient.unsubscribe(TOPIC); mqttAndroidClient.disconnect(); } } catch (Exception e) { Log.d(TAG, "! Disconnect failed!"); e.printStackTrace(); } Log.d(TAG, "onDestroy Service!"); } I call function to stop service. stopService(new Intent(getActivity(), MyService.class));_ But sometime MQTT in service can't stop logcat still does not stop working. Sample: Register alarmreceiver to MqttServiceMqttService.pingSender.Android478 D/AlarmPingSender: Unregister alarmreceiver to MqttServiceAndroid478 D/AlarmPingSender: Schedule next alarm at 1503748388874 D/AlarmPingSender: Alarm scheule using setExactAndAllowWhileIdle, next: 60000 D/AlarmPingSender: Schedule next alarm at 1503748328982


Sory i can't use tag ^^

nguyenlinhnttu avatar Aug 26 '17 11:08 nguyenlinhnttu

Stop service and cause leakage

logcat : 2019-06-06 15:20:51.542 18926-18926/com.example.myapplication E/ActivityThread: Service com.example.myapplication.MQttService1 has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@f277926 that was originally bound here android.app.ServiceConnectionLeaked: Service com.example.myapplication.MQttService1 has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@f277926 that was originally bound here at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:1532) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1424) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1605) at android.app.ContextImpl.bindService(ContextImpl.java:1557) at android.content.ContextWrapper.bindService(ContextWrapper.java:684) at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:425) at com.example.myapplication.MQttService1.doClientConnection(MQttService1.java:138) at com.example.myapplication.MQttService1.init(MQttService1.java:129) at com.example.myapplication.MQttService1.onCreate(MQttService1.java:181) at android.app.ActivityThread.handleCreateService(ActivityThread.java:3339) at android.app.ActivityThread.-wrap4(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

I call function to stop service. stopService(new Intent(getActivity(), MQttService1.class));

Please check code MQttService1.java

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy: "+"servicer" );
    try {
        client.unsubscribe(topicArray);
        client.disconnect();
    } catch (MqttException e) {
        e.printStackTrace();
    }
    //stopSelf();
    super.onDestroy();
}

LinYh08 avatar Jun 06 '19 07:06 LinYh08

I found that not waiting for MqttService to disconnect will cause leaks; I'm currently using

public void destroyConnection() {
    try {
        client.unregisterResources();
        client.disconnect(0);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

By calling client.disconnect(0); you won't leave Paho any wait time, and will therefore disconnect immediatly - or at least that's what it seems to do. This way I have no leaks anymore.

davidevertuani avatar Jun 08 '19 09:06 davidevertuani

Hi @davidevertuani, I try your solution, but it still run. :( Schedule next alarm at 1589079675068

nguyenvanquan7826 avatar May 10 '20 03:05 nguyenvanquan7826

Hi, I also ran into that leaked ServiceConnection issue. The following disconnect setup fixed it for me:

mqttClient?.disconnect(null, object : IMqttActionListener {
    override fun onSuccess(asyncActionToken: IMqttToken?) {
        println("Mqtt disconnect onSuccess")
        mqttClient?.unregisterResources()
        mqttClient?.setCallback(null)
        mqttClient?.close()
        mqttClient = null
    }

    override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
        println("Mqtt disconnect onFailure")
    }
})

wman1980 avatar Sep 05 '23 07:09 wman1980