Skip to content

Commit f7d51a8

Browse files
author
Cameron Auser
committed
Explicitly close the socket when resetting the client if the connection is not yet opened. This prevents the write thread from hanging while attempting to write to the output stream. Reset the connection if we fail to connect during connectBlocking.
1 parent 6910229 commit f7d51a8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ 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+
if (engine.getReadyState() == ReadyState.NOT_YET_CONNECTED) {
343+
socket.close();
344+
}
342345
closeBlocking();
346+
343347
if (writeThread != null) {
344348
this.writeThread.interrupt();
345349
this.writeThread.join();
@@ -401,7 +405,13 @@ public boolean connectBlocking() throws InterruptedException {
401405
*/
402406
public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException {
403407
connect();
404-
return connectLatch.await(timeout, timeUnit) && engine.isOpen();
408+
409+
boolean connected = connectLatch.await(timeout, timeUnit);
410+
if (!connected) {
411+
reset();
412+
}
413+
414+
return connected && engine.isOpen();
405415
}
406416

407417
/**

0 commit comments

Comments
 (0)