Skip to content

Commit c6458db

Browse files
authored
Merge pull request #245 from dobermai/fix_deadlock_issue_244
fixed deadlock that was caused by joining the receiver thread in the …
2 parents 4714343 + d97cc69 commit c6458db

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)