aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 550 forks source link

IoT MQTT Client stuck disconnecting #292

Closed AngusMorton closed 5 years ago

AngusMorton commented 7 years ago
  1. Call AWSIotMqttManager.connect(credentialsProvider, callback).
  2. Then call AWSIotMqttManager.disconnect().
  3. If the error happens then subsequent connect() calls fail with the "Client is currently disconnecting" error.

After some relatively large period of time (> 1 minute) calling connect seems to start working again.

Client is currently disconnecting (32102)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:536)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.mqttConnect(AWSIotMqttManager.java:729)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.access$900(AWSIotMqttManager.java:59)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager$1.run(AWSIotMqttManager.java:692)
at java.lang.Thread.run(Thread.java:818)
bxpan commented 7 years ago

Hi AngusMorton,

In step3, when you say "if error happens", what exact error are you getting, is there any error message ?

AngusMorton commented 7 years ago

Yes, I'm getting the exception:

Client is currently disconnecting (32102)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:536)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.mqttConnect(AWSIotMqttManager.java:729)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.access$900(AWSIotMqttManager.java:59)
at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager$1.run(AWSIotMqttManager.java:692)
at java.lang.Thread.run(Thread.java:818)
bxpan commented 7 years ago

Hi AngusMorton,

I tried out using one of the aws-sdk-android-sample application, but didn't observe this issue. Is there a particular sequence of steps that triggers this error? How often does this error occur to you? Did it happen every time? Also, if you have a log , please also paste that.

Thanks.

AngusMorton commented 7 years ago

It seems to happen in cases where I connect very quickly after disconnecting (as in, an automatic connect call after being disconnected).

I'll put together a small repo that reproduces the issue over the weekend.

AngusMorton commented 7 years ago

It occurs because mqttClient.disconnect() doesn't sever the underlying connection immediately. Instead, it waits for a quiesce timeout and attempts to disconnect cleanly.

What this means is that if you call MqttAsyncClient.disconnect() and then call MqttAsyncClient.connect() before the underlying mqttClient has performed the clean disconnection it will throw the Client is currently disconnecting exception.

mutablealligator commented 7 years ago

Hi @AngusMorton,

Thanks for sharing the reproduction steps. We will try to reproduce the error and will work on a fix is the issue persists.

hs8058 commented 7 years ago

Added to this, we also observed the same reaction. Here is the error. In terms of the sequence to re-create it, we are using IOT for an app that needs constant connection with 120 seconds of keepalive and auto-reconnect set to true. We have observed that sometimes, so far apparently randomly, the connection simply and silently dies off without any retries. It's hard to reproduce because it only happens when on 4G or 3G network on a mobile device not in normal dev environment.

: Status = Connecting : Status = ConnectionLost : Connection error. Client is currently disconnecting (32102) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:536) at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.mqttConnect(AWSIotMqttManager.java:724) at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.access$800(AWSIotMqttManager.java:56) at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager$1.run(AWSIotMqttManager.java:688) at java.lang.Thread.run(Thread.java:818)

satyasivaprasad commented 6 years ago

Facing same issue while switching between wifi and mobile data IoT MQTT Client stuck in disconnecting state.

Did any one got solution for this issue ?

desokroshan commented 5 years ago

@satyasivaprasad Are you running this on an emulator or on a real device?

hs8058 commented 5 years ago

This was a real device.

On Wed, 16 Jan 2019 01:39 Roshan Kumar <notifications@github.com wrote:

@satyasivaprasad https://github.com/satyasivaprasad Are you running this on an emulator or on a real device?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/aws-amplify/aws-sdk-android/issues/292#issuecomment-454437024, or mute the thread https://github.com/notifications/unsubscribe-auth/AWN8_-slgvSL3twTdULgLvGaOTMG9nbkks5vDfYUgaJpZM4NJ0s5 .

sushantbisht commented 5 years ago

I have the exact same Issue. I am using my Android phone as emulator. Whenever my phone is on wifi mode, the connection is happening and the publishing is happening successfully. Whenever i change it to 4G, no connection is happening and i am getting the following error

: Publish error.
 com.amazonaws.AmazonClientException: Client is disconnected or not yet connected.
        at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishData(AWSIotMqttManager.java:1242)
        at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishData(AWSIotMqttManager.java:1191)
        at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishString(AWSIotMqttManager.java:1151)**
yogeshsingh01 commented 5 years ago

Hi, I have also same issue I/AWSIotMqttManager: metrics collection is enabled, username: ?SDK=Android&Version=2.13.2 05-05 00:40:20.013 26351-26351/com.example.myiotapp I/AWSIotMqttManager: resetting reconnect attempt and retry time 05-05 00:40:20.017 26351-26351/com.example.myiotapp D/com.example.myiotapp.MainActivity: Status = Connecting 05-05 00:40:20.029 26351-26351/com.example.myiotapp E/com.example.myiotapp.MainActivity: Publish error. com.amazonaws.AmazonClientException: Client is disconnected or not yet connected. at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishData(AWSIotMqttManager.java:1308) at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishData(AWSIotMqttManager.java:1257) at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.publishString(AWSIotMqttManager.java:1217) at com.example.myiotapp.MainActivity.publishClick(MainActivity.java:157) at com.example.myiotapp.MainActivity$4.onClick(MainActivity.java:212) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21153) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

mutablealligator commented 5 years ago

When you call AWSIotMqttManager.disconnect(), we invoke MqttAsyncClient.disconnect(0) where 0 is the quiesce timeout. So there is no delay before attempting to disconnect the connection in Paho. If the disconnect attempt is successful, a token is returned and we set the state to ConnectionLost and notify the connection callback. In other cases, we would throw an exception.

1) For the error Client is currently disconnecting (32102), I recommend creating a new connection through AWSIotMqttManager.connect() only when a successful callback of disconnect is returned.

2) For the error : Publish error. com.amazonaws.AmazonClientException: Client is disconnected or not yet connected. you need to call AWSIotMqttManager.connect() before attempting to publish. If the device is offline, the connection will be lost if it exceeds the KeepAlive timeout. So I recommend to increase the KeepAlive timeout for intermittent connectivity failures. For longer network loss that exceeds the KeepAlive timeout, you need to ensure a valid connection is present before publishing the data.

We have also commented on Paho repository on the issues related to disconnect.