Skip to content

Client blocking connect and close methods return too soon #302

@dhumeniuk

Description

@dhumeniuk

In WebSocketClient, the connect/close latches will count down before the callback is called:

    public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
    connectLatch.countDown();
    onOpen( (ServerHandshake) handshake );
}

This means the blocking methods will return before the callback is called. If the consumer of the client interface does something on open that is required before proceeding, there is a race condition where the callback action may not happen soon enough. For example:

public class ClientConsumer {
  @Override
  public void onOpen(final ServerHandshake handshake) {
    m_Log.info("Connected to MessageServer: %s", getURI());
    m_Connected = true;
  }

  public connect() {    
    client.connectBlocking();
  }

  public send() {
    if (m_Connected) {
      client.send(...);
    }
  }

ClientConsumer c;
c.connect();  // will return before m_Connect is set possibly
c.send(); // will not always send since m_Connect may still be false

This is a simplified example. Our actual wrapper code has more to it.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions