Skip to content

Commit

Permalink
Merge pull request #1399 from ysi-camerona/ISSUE-1397
Browse files Browse the repository at this point in the history
Have connectBlocking clean up after a timeout
  • Loading branch information
PhilipRoman committed Feb 24, 2024
2 parents 6910229 + d8eb0f8 commit aa84b39
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/java/org/java_websocket/client/WebSocketClient.java
Expand Up @@ -339,7 +339,13 @@ private void reset() {
"You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to ensure a successful cleanup.");
}
try {
// 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
if (engine.getReadyState() == ReadyState.NOT_YET_CONNECTED && socket != null) {
// Closing the socket when we have not connected prevents the writeThread from hanging on a write indefinitely during connection teardown
socket.close();
}
closeBlocking();

if (writeThread != null) {
this.writeThread.interrupt();
this.writeThread.join();
Expand Down Expand Up @@ -401,7 +407,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 aa84b39

Please sign in to comment.