hivemq/hivemq-mqtt-client

Android | ANR while creating & conencting MQTT3 client

pravingws opened this issue ยท 1 comments

๐Ÿ› Bug Report

๐Ÿ”ฌ How To Reproduce

Steps to reproduce the behavior:

  1. Not sure on how to reproduce but it is this ANR is reported by many users.

Code sample

`private fun createMqttClient() {
logMqtt("WSConnMgr :: createMqttClient() >> $clientIdentifier")

    if (mqttCredsHandler.isValidData()) {
        val websocketConfig = MqttWebSocketConfig.builder().serverPath(MQTT_SERVER_PATH).build()

        // refer: https://stackoverflow.com/questions/71495372/hivemq-java-library-fails-to-automatically-reconnect-to-broker
        mqtt3Client = Mqtt3Client
                .builder()
                .identifier(clientIdentifier)  // The unique identifier of the MQTT client
                .serverHost(mqttCredsHandler.getMqttUrl()) // The host name or IP address of the MQTT server
                .serverPort(mqttCredsHandler.getMqttPortNumber()) // The address (host + port) of the MQTT server
                .webSocketConfig(websocketConfig) //WebSocket transport configuration
                .sslWithDefaultConfig() // Secure transport configuration (SSL/TLS)
                // .automaticReconnectWithDefaultConfig()  // Default -startInitDelay is 1s and maxDelay is 120s
                .automaticReconnect() // Lifecycle configuration - Automatic reconnect configuration
                .initialDelay(1, TimeUnit.SECONDS) // Sets the initial delay the client will wait before it tries to reconnect
                .maxDelay(2, TimeUnit.SECONDS) // Sets the maximum delay the client will wait before it tries to reconnect.
                .applyAutomaticReconnect() // Lifecycle configuration - Automatic reconnect configuration
                .simpleAuth()
                .username(mqttCredsHandler.getMqtttUserName())
                .password(mqttCredsHandler.getMqttPasswordByteArray())
                .applySimpleAuth()
                .addConnectedListener { context ->
                    logMqtt("WSConnMgr ::CONNECT SUCCESS  onConnected() >>: context = $context")
                    handleListenerQueue()
                    reAttemptCounter = 0
                }
                .addDisconnectedListener { context ->
                    logMqtt("WSConnMgr :: mqttDisconnectConnectionListener callback: " +
                            "\n D-Listeners: " + wsListenersList.size +
                            "\n D-Message: " + context.cause.message +
                            "\n D-source: " + context.source.toString() +
                            "\n D-State: " + context.clientConfig.state.toString() +
                            "\n D-Cause: " + context.cause, "e")
                    handleMqttDisconnect(context)
                }.buildAsync()

        try {
            val connAckFuture = mqtt3Client!!
                    .connectWith()
                    .keepAlive(MQTT_KEEP_ALIVE_SEC)
                    .cleanSession(false)
                    .send()

            logMqtt("WSConnMgr ::connAckFuture >>: context = ${connAckFuture.get().returnCode}")
        } catch (e: Exception) {
            logMqtt("WSConnMgr ::connAckFuture >>: Error: " + e.localizedMessage, "e")
        }

    } else {
        logMqtt("WSConnMgr ::connAckFuture FAILURE. MQTT CREDS NOT FOUND. NOT ATTEMPTING TO CONNECT !!", "e")
    }
}`

Environment

Where are you running/using this client?

Hardware or Device? ANDROID OS version 11 & above

What version of this client are you using? MQTT3 & dependency version 1.3.1

JVM version?

Operating System? ANDROID

Which MQTT protocol version is being used? MQTT3

Which MQTT broker (name and version)? hive and 1.3.1

Screenshots - NA

๐Ÿ“ˆ Expected behavior

  • App should not report ANR or related issue while creating a client

๐Ÿ“Ž Additional context - Stacktrace

ANR stacktrace from Playconsole app vitals:
at jdk.internal.misc.Unsafe.park (Native method) at java.util.concurrent.locks.LockSupport.park (LockSupport.java:211) at java9.util.concurrent.CompletableFuture$Signaller.block (CompletableFuture.java:1861) at java9.util.concurrent.ForkJoinPool.managedBlock (ForkJoinPool.java:3231) at java9.util.concurrent.CompletableFuture.waitingGet (CompletableFuture.java:1888) at java9.util.concurrent.CompletableFuture.get (CompletableFuture.java:2063) at com.wrkspot.employee.common.mqtt.WSMqttConnectionManager.createMqttClient (WSMqttConnectionManager.java:136) at com.wrkspot.employee.common.mqtt.WSMqttConnectionManager.initiateMqttConnection (WSMqttConnectionManager.java:70) at com.wrkspot.employee.backgroundservice.WrkspotMqttService.onCreate (WrkspotMqttService.java:107) at android.app.ActivityThread.handleCreateService (ActivityThread.java:4641) at android.app.ActivityThread.access$1800 (ActivityThread.java:262) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2174) at android.os.Handler.dispatchMessage (Handler.java:111) at android.os.Looper.loopOnce (Looper.java:238) at android.os.Looper.loop (Looper.java:357) at android.app.ActivityThread.main (ActivityThread.java:8111) at java.lang.reflect.Method.invoke (Native method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1026)

Plain text logs
stacktrace (1).log