TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

NPE during reconnect #1364

Closed Xander-Polishchuk closed 10 months ago

Xander-Polishchuk commented 11 months ago

Describe the bug It's possible to get NPE in reconnectBlocking(), due to a race with previously running client connection thread.

The race happens due to closeLatch countdown called before connectReadThread is set to null in run

To Reproduce Steps to reproduce the behavior:

  1. Setup WS server
  2. Connect
  3. Call reconnectBlocking() in a loop until NPE

Expected behavior No NPE on reconnectBlocking()

Debug log

java.lang.NullPointerException: Cannot invoke "java.lang.Thread.start()" because "this.connectReadThread" is null
    at org.java_websocket.client.WebSocketClient.connect(WebSocketClient.java:376)
    at org.java_websocket.client.WebSocketClient.connectBlocking(WebSocketClient.java:386)
    at org.java_websocket.client.WebSocketClient.reconnectBlocking(WebSocketClient.java:328)
    at org.web3j.protocol.websocket.WebSocketService.connectToWebSocket(WebSocketService.java:140)

Environment(please complete the following information):

Xander-Polishchuk commented 11 months ago

While was trying to reproduce I identified another bug with WebSocketClient#closeBlocking, it sometimes gets stuck on closeLatch.await(), I'm going to create another bug for that.