Skip to content

Commit

Permalink
Explicitly close the socket when resetting the client if the connecti…
Browse files Browse the repository at this point in the history
…on 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.
  • Loading branch information
ysi-camerona committed Feb 16, 2024
1 parent 6910229 commit f7d51a8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ private void reset() {
"You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to ensure a successful cleanup.");
}
try {
if (engine.getReadyState() == ReadyState.NOT_YET_CONNECTED) {
socket.close();
}
closeBlocking();

if (writeThread != null) {
this.writeThread.interrupt();
this.writeThread.join();
Expand Down Expand Up @@ -401,7 +405,13 @@ public boolean connectBlocking() throws InterruptedException {
*/
public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException {
connect();
return connectLatch.await(timeout, timeUnit) && engine.isOpen();

boolean connected = connectLatch.await(timeout, timeUnit);
if (!connected) {
reset();
}

return connected && engine.isOpen();
}

/**
Expand Down

0 comments on commit f7d51a8

Please sign in to comment.