{"record":{"id":"2baa4459912c1b5e","repo":"TooTallNate/Java-WebSocket","slug":"you-cannot-initialize-a-reconnect-out-of-the-webso","errorCode":null,"errorMessage":"You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to ensure a successful cleanup.","messagePattern":"You cannot initialize a reconnect out of the websocket thread\\. Use reconnect in another thread to ensure a successful cleanup\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/client/WebSocketClient.java","lineNumber":353,"sourceCode":"   * @param timeUnit The timeout time unit\n   * @return Returns whether it succeeded or not.\n   * @throws InterruptedException Thrown when the threads get interrupted\n   * @since 1.6.1\n   */\n  public boolean reconnectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException {\n    reset();\n    return connectBlocking(timeout, timeUnit);\n  }\n\n  /**\n   * Reset everything relevant to allow a reconnect\n   *\n   * @since 1.3.8\n   */\n  private void reset() {\n    Thread current = Thread.currentThread();\n    if (current == writeThread || current == connectReadThread) {\n      throw new IllegalStateException(\n          \"You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to ensure a successful cleanup.\");\n    }\n    try {\n      // 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\n      if (engine.getReadyState() == ReadyState.NOT_YET_CONNECTED && socket != null) {\n        // Closing the socket when we have not connected prevents the writeThread from hanging on a write indefinitely during connection teardown\n        socket.close();\n      }\n      closeBlocking();\n\n      if (writeThread != null) {\n        this.writeThread.interrupt();\n        this.writeThread.join();\n        this.writeThread = null;\n      }\n      if (connectReadThread != null) {\n        this.connectReadThread.interrupt();\n        this.connectReadThread.join();","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/client/WebSocketClient.java#L335-L371","documentation":"Guard in WebSocketClient.reset(): reconnect/reconnectBlocking call reset(), which interrupts and joins the write and connect-read threads. If invoked from the websocket's own writeThread or connectReadThread, joining itself would deadlock, so the call is rejected. This is a caller-context check, not a data validation error.","triggerScenarios":"Thrown at src/main/java/org/java_websocket/client/WebSocketClient.java:353 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call reconnect() or reconnectBlocking() from your own application thread, never from onMessage/onError/onClose or other websocket-thread callbacks.","Schedule the reconnect on a separate executor/thread, e.g. `new Thread(client::reconnect).start()`.","Use a scheduler (ScheduledExecutorService) with a delay to reconnect after the callback returns."],"exampleFix":null,"handlingStrategy":"try-catch","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"}