File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed
src/main/java/org/java_websocket/client Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments