Skip to content

Risk of race-condition when calling reconnect() #1470

@oliviermartin

Description

@oliviermartin

Describe the bug
When connecting to a really slow websocket server, we could have a race condition when reconnect() is called before the websocket has been connected by the first connect(). With this race condition, there is a risk the websocket thread hang.

To Reproduce

  1. Have a slow Websocket server (I am not familiar enough with Websocket protocol but maybe it could be duplicated with this project when Websocket server only send only the first few packets)
  2. Create a Websocket-client instance
  3. Call connect()
  4. Call reconnect() - the reconnect() should be sent before the connection has been established.

Expected behavior
The reconnect() can connect.

Debug log

Log for the first connect():

05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: [WebSocketConnectReadThread-8083] - write(350): GET /api/v1/gateway HTTP/1.1
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Authorization: token xxxxxxxxxxxxx
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Connection: Upgrade
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Host: my-slow-websocket-server.com
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Origin: http://my-slow-websocket-server.com
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Sec-WebSocket-Key: 62fFYn1dkN8LyfAU6GSe6g==
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Sec-WebSocket-Version: 13
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: Upgrade: websocket
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: User-Agent: My Test App
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: 
05-19 23:45:12.359 16324 17177 V org.java_websocket.WebSocketImpl: 
05-19 23:45:12.436 16324 17177 V org.java_websocket.WebSocketImpl: [WebSocketConnectReadThread-8083] - process(5): (HTTP/)

Call reconnect():

No log - hang!

Environment(please complete the following information):

  • Version used: v1.5.5
  • Java version: Java 11
  • Operating System and version: Android 11

Additional context
I have done some investigation... reconnect() calls reset() and connect(). The issue is reset() is actually blocking by calling closeBlocking() in close(). I check when the problem occurs readyState == NOT_YET_CONNECTED when we get into close().
The case readyState == NOT_YET_CONNECTED should potentially be handled in close().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions