Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MQTT Eclipse Paho client on Android, disconnect hangs and never completes

Tags:

android

push

mqtt

I'm using MQTT in an android application, using the latest jar as found on the Eclipse Paho page.

I'm manually checking the connection by sending occasional publishes under a QOS of 1, and if it's not delivered under a certain timeout I disconnect MQTT and reconnect. When the data connection is lost then regained I also force a reconnect.

The issue is that the disconnect method hangs the thread and never completes - even with a zero or negative timeout. It's supposed to have an internal timeout of 30 seconds but it goes long past this and never completes.

The MqttClient object needs to be disconnected otherwise it sits in the background chewing up data. What I'm now doing is spinning off a seperate thread to disconnect MQTT then creating a new MqttClient entirely. It stops using data but it's a bad solution as the new thread just hangs instead.

Any ideas?

Edit: On further investigation it seems to be blocking on attempting to stop CommsReceiver at line 70:

public void stop() throws IOException {
    synchronized (lifecycle) {
        //@TRACE 850=stopping receiver
        trace.trace(Trace.FINE,850);
        if (running) {
            running = false;
            try {
                //@TRACE 851=stop: wait on lifecycle
                trace.trace(Trace.FINE,851);
                // Wait for the thread to finish.
                lifecycle.wait(); // THREAD HANGS //
            }
            catch (InterruptedException ex) {
            }
        }
    }
}
like image 966
user705142 Avatar asked Nov 12 '12 03:11

user705142


2 Answers

The problem is due to a timing window in the stable version of the paho client, if you use the code in the develop branch of the paho repository. You shouldn't come across this problem because it is marked as fixed in this bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=394066 although its not the stable branch I have found the develop branch to be reliable.

like image 105
rhyshort Avatar answered Oct 05 '22 13:10

rhyshort


There is a bug filed here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=394066 (probably by the OP of this question, info for @Vipul). The status says resolved so see when the new JAR is available with the fix and use that

like image 22
Vrashabh Irde Avatar answered Oct 05 '22 12:10

Vrashabh Irde