{"record":{"id":"ef5c337091c14b5d","repo":"TooTallNate/Java-WebSocket","slug":"websocketclient-objects-are-not-reuseable","errorCode":null,"errorMessage":"WebSocketClient objects are not reuseable","messagePattern":"WebSocketClient objects are not reuseable","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/client/WebSocketClient.java","lineNumber":394,"sourceCode":"        this.socket.close();\n        this.socket = null;\n      }\n    } catch (Exception e) {\n      onError(e);\n      engine.closeConnection(CloseFrame.ABNORMAL_CLOSE, e.getMessage());\n      return;\n    }\n    connectLatch = new CountDownLatch(1);\n    closeLatch = new CountDownLatch(1);\n    this.engine = new WebSocketImpl(this, this.draft);\n  }\n\n  /**\n   * Initiates the websocket connection. This method does not block.\n   */\n  public void connect() {\n    if (connectReadThread != null) {\n      throw new IllegalStateException(\"WebSocketClient objects are not reuseable\");\n    }\n    connectReadThread = new Thread(this);\n    connectReadThread.setDaemon(isDaemon());\n    connectReadThread.setName(\"WebSocketConnectReadThread-\" + connectReadThread.getId());\n    connectReadThread.start();\n  }\n\n  /**\n   * Same as <code>connect</code> but blocks until the websocket connected or failed to do so.<br>\n   *\n   * @return Returns whether it succeeded or not.\n   * @throws InterruptedException Thrown when the threads get interrupted\n   */\n  public boolean connectBlocking() throws InterruptedException {\n    connect();\n    connectLatch.await();\n    return engine.isOpen();\n  }","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/client/WebSocketClient.java#L376-L412","documentation":"Thrown by WebSocketClient.connect() when connectReadThread is still non-null, meaning connect() (or a reconnect) was already called and the previous connection lifecycle has not been torn down. WebSocketClient is single-use per connect cycle: it keeps references to its threads and latches, so a second connect on the same instance is rejected instead of corrupting state.","triggerScenarios":"Thrown at src/main/java/org/java_websocket/client/WebSocketClient.java:394 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create a new WebSocketClient instance for each (re)connection instead of calling connect() twice.","Use client.reconnect() or reconnectBlocking(), which reset the internal state and create fresh latches and engine.","Ensure a prior connect failed fully (threads joined) before re-attempting; otherwise the stale connectReadThread triggers this error."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}