eclipse-paho / eclipse-paho/paho.mqtt.java
MQTTAsyncClient stuck on broken broker IP — reconnect(), disconnect() don't work
- Dominant language
- Java
- Stars
- 2.3k
- Forks
- 919
- PR merge metrics
- No merged PRs in 30d
Description
I'm working on an Android app using Eclipse Paho's MqttAsyncClient with SSL and multiple broker IPs. The idea is to randomly connect to one of the nodes:
```
fun connect() {
val sslContext = settingsUseCase.getSSLContext() ?: return
val brokerIPs = settingsUseCase.getBrokerIPs()
val clientUID = settingsUseCase.getUID() ?: return
if (brokerIPs.isEmpty()) return
mqttClient = MqttAsyncClient(brokerIPs.random(), clientUID, MemoryPersistence()).apply {
setCallback(MqttCallbacks())
connect(
MQTTUtils.getSSLMQTTOptions(sslContext, brokerIPs),
null,
MqttConnectListener()
)
}
}
```
The SSL options look like this:
```
fun getSSLMQTTOptions(
sslContext: SSLContext,
serverURIs: Array
): MqttConnectOptions {
val sslSocketFactory = sslContext.socketFactory
return MqttConnectOptions().apply {
isAutomaticReconnect = true
isCleanSession = false
connectionTimeout = 60
keepAliveInterval = 120
mqttVersion = MqttConnectOptions.MQTT_VERSION_3_1_1
socketFactory = sslSocketFactory
serverURIs = serverURIs
}
}
```
### Problem
Sometimes the client connects to a broken node. When this happens:
- The connection hangs silently — no logs from `connectionLost()` or `connectComplete()`.
- `connectionTimeout` appears to have no effect; the process is stuck indefinitely.
- `disconnect()`, `close()`, and `reconnect()` all result in an `"Already connected"` exception.
- Only `disconnectForcibly()` doesn't cause an exception, but does nothing.
- The only workaround is physically disabling the network (e.g. turning off Wi-Fi), which finally forces a disconnect.
---
### Question
Is there a reliable way to **forcefully cancel or kill** the `MqttAsyncClient` when it's stuck like this?
Ideally I’d want to do one of the following:
- Cancel the pending connection attempt.
- Retry with a different IP.
- Forcefully reset the client state.
Any suggestions or known issues with Paho’s `MqttAsyncClient` in this context?
---
### What I've tried
- `mqttClient?.disconnectForcibly()`
- Rebuilding a new `MqttAsyncClient` instance (still hangs if previous connection is stuck)
- Setting smaller `connectionTimeout` (ignored)
Contributor guide
Assessment
This issue has not been assessed yet.