eclipse-paho / eclipse-paho/paho.mqtt.java

Reconnecting not working for MqttAsyncClient w/ file persistence, offline buffering, and QOS 2 publishes

Open
#1,025 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

- [x] Bug exists Release Version 1.2.5 ( Master Branch)
- [ ] Bug exists in MQTTv3 Client on Snapshot Version 1.2.6-SNAPSHOT (Develop Branch)
- [ ] Bug exists in MQTTv5 Client on Snapshot Version 1.2.6-SNAPSHOT (Develop Branch)

My use case requires a publishing MQTT client with file based persistence and offline buffering. The client publishes QOS 2 messages to a broker on the network. When the connection is disrupted (broker goes down, network disconnect, etc.), the client reconnects, attempts to publish a messages, and then disconnects again. This behavior cycles, with none of the messages in the buffer getting cleared. Below is a client setup that was able to replicate the behavior:
```
package mqtt_test;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

public class Main {
public static void main(String[] args) {
String broker = "tcp://10.102.11.26:1883";
String clientId = UUID.randomUUID().toString();
try {
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setMaxInflight(500);
mqttConnectOptions.setCleanSession(false);
mqttConnectOptions.setConnectionTimeout(5);

DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
disconnectedBufferOptions.setBufferEnabled(true);
disconnectedBufferOptions.setBufferSize(5000);
disconnectedBufferOptions.setPersistBuffer(true);
disconnectedBufferOptions.setDeleteOldestMessages(true);

MqttDefaultFilePersistence persistence = new MqttDefaultFilePersistence("C:\\Users\\user\\IdeaProjects\\mqtt_test\\src");

MqttAsyncClient client = new MqttAsyncClient(broker, clientId, persistence);

client.setBufferOpts(disconnectedBufferOptions);
client.connect(mqttConnectOptions);
for(int i = 0; i < 10000; i++) {
TimeUnit.MILLISECONDS.sleep(100);
byte[] payload = "test".getBytes("UTF-16");
client.publish("topic1", payload, 2, false);
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}

}

```
The following error is printed to the console:
SEVERE: null: Error occurred attempting to publish buffered message due to disconnect. Exception: 32,102:Client is currently disconnecting.
Dec 08, 2023 2:38:42 PM org.eclipse.paho.client.mqttv3.internal.DisconnectedMessageBuffer run
SEVERE: null: Error occurred attempting to publish buffered message due to disconnect. Exception: 32,104:Client is not connected.
Dec 08, 2023 2:38:43 PM org.eclipse.paho.client.mqttv3.internal.DisconnectedMessageBuffer run

On the broker side, I am using the Aedes broker for Node-RED v0.11.0. From the broker I get the following error: "Error: no such packet"

Contributor guide

Open the contributing guide

Research direction

Start with the supplied MqttAsyncClient reproduction and the internal DisconnectedMessageBuffer path named in the log, then trace reconnect and buffered publish handling for QoS 2 messages. Reproduce the disconnect loop and verify that buffered messages drain after reconnection without repeated “Client is currently disconnecting” or “not connected” errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.