Skip to content

Commit aa84b39

Browse files
authored
Merge pull request TooTallNate#1399 from ysi-camerona/ISSUE-1397
Have connectBlocking clean up after a timeout
2 parents 6910229 + d8eb0f8 commit aa84b39

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,13 @@ private void reset() {
339339
"You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to ensure a successful cleanup.");
340340
}
341341
try {
342+
// This socket null check ensures we can reconnect a socket that failed to connect. It's an uncommon edge case, but we want to make sure we support it
343+
if (engine.getReadyState() == ReadyState.NOT_YET_CONNECTED && socket != null) {
344+
// Closing the socket when we have not connected prevents the writeThread from hanging on a write indefinitely during connection teardown
345+
socket.close();
346+
}
342347
closeBlocking();
348+
343349
if (writeThread != null) {
344350
this.writeThread.interrupt();
345351
this.writeThread.join();
@@ -401,7 +407,13 @@ public boolean connectBlocking() throws InterruptedException {
401407
*/
402408
public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException {
403409
connect();
404-
return connectLatch.await(timeout, timeUnit) && engine.isOpen();
410+
411+
boolean connected = connectLatch.await(timeout, timeUnit);
412+
if (!connected) {
413+
reset();
414+
}
415+
416+
return connected && engine.isOpen();
405417
}
406418

407419
/**

0 commit comments

Comments
 (0)