Skip to content

Commit d97cc69

Browse files
committed
fixed deadlock that was caused by joining the receiver thread in the synchronized block of the lifecycle. The bug caused Paho to wait forever when there was a socket exception after the close was initiated (e.g. when the broker closed the connection prematurely or when something bad happenes on the network)
Signed-off-by: Dominik Obermaier <dominik.obermaier@gmail.com>
1 parent 31bcfcc commit d97cc69

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

  • org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketReceiver.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,25 @@ public void start(String threadName){
6565
public void stop() {
6666
final String methodName = "stop";
6767
stopping = true;
68+
boolean closed = false;
6869
synchronized (lifecycle) {
6970
//@TRACE 850=stopping
7071
log.fine(CLASS_NAME,methodName, "850");
7172
if(running) {
7273
running = false;
7374
receiving = false;
75+
closed = true;
7476
closeOutputStream();
75-
if( !Thread.currentThread().equals(receiverThread)) {
76-
try {
77-
// Wait for the thread to finish
78-
receiverThread.join();
79-
} catch (InterruptedException ex) {
80-
// Interrupted Exception
81-
}
82-
}
77+
78+
}
79+
}
80+
if(closed && !Thread.currentThread().equals(receiverThread)) {
81+
try {
82+
// Wait for the thread to finish
83+
//This must not happen in the synchronized block, otherwise we can deadlock ourselves!
84+
receiverThread.join();
85+
} catch (InterruptedException ex) {
86+
// Interrupted Exception
8387
}
8488
}
8589
receiverThread = null;

0 commit comments

Comments
 (0)