Skip to content

Commit 7e13cdf

Browse files
author
Ian Craggs
committed
Fix and test for issue #330 - hang in disconnect
1 parent 4b08272 commit 7e13cdf

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

  • org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test
  • org.eclipse.paho.client.mqttv3/src/main/java-templates/org/eclipse/paho/client/mqttv3/internal

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/BasicTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.eclipse.paho.client.mqttv3.IMqttClient;
2323
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
24+
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
2425
import org.eclipse.paho.client.mqttv3.MqttCallback;
2526
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
2627
import org.eclipse.paho.client.mqttv3.MqttException;
@@ -326,6 +327,33 @@ public void testConnOptDefaults() throws Exception {
326327
Assert.assertNull(connOpts.getSSLProperties());
327328
}
328329

330+
331+
@Test
332+
public void test330() throws Exception {
333+
String methodName = Utility.getMethodName();
334+
LoggingUtilities.banner(log, cclass, methodName);
335+
336+
URI uri = new URI("tcp://iot.eclipse.org:1882");
337+
IMqttAsyncClient client = clientFactory.createMqttAsyncClient(uri, "client-1");
338+
339+
MqttConnectOptions options = new MqttConnectOptions();
340+
options.setAutomaticReconnect(true);
341+
options.setUserName("foo");
342+
options.setPassword("bar".toCharArray());
343+
options.setConnectionTimeout(2);
344+
client.connect(options);
345+
346+
Thread.sleep(1000);
347+
348+
try {
349+
// this would deadlock before fix
350+
client.disconnect(0).waitForCompletion();
351+
} finally {
352+
client.close();
353+
}
354+
}
355+
356+
329357
// -------------------------------------------------------------
330358
// Helper methods/classes
331359
// -------------------------------------------------------------

org.eclipse.paho.client.mqttv3/src/main/java-templates/org/eclipse/paho/client/mqttv3/internal/ClientComms.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,12 +766,19 @@ public void run() {
766766
clientState.quiesce(quiesceTimeout);
767767
try {
768768
internalSend(disconnect, token);
769-
token.internalTok.waitUntilSent();
769+
// do not wait if the sender process is not running
770+
if (sender != null && sender.isRunning()) {
771+
token.internalTok.waitUntilSent();
772+
}
770773
}
771774
catch (MqttException ex) {
772775
}
773776
finally {
774777
token.internalTok.markComplete(null, null);
778+
if (sender == null || !sender.isRunning()) {
779+
// if the sender process is not running
780+
token.internalTok.notifyComplete();
781+
}
775782
shutdownConnection(token, null);
776783
}
777784
}

0 commit comments

Comments
 (0)