Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client blocking connect and close methods return too soon #302

Closed
dhumeniuk opened this issue Jan 23, 2015 · 5 comments
Closed

Client blocking connect and close methods return too soon #302

dhumeniuk opened this issue Jan 23, 2015 · 5 comments
Assignees
Milestone

Comments

@dhumeniuk
Copy link

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.

@petertorelli
Copy link

This can also be observed in the example client by calling c.send() immediately after c.connect(); calling .isOpen right after .connect() always returns false when testing from the console, but a slight delay solves the problem.

@gesellix
Copy link

I can confirm the issue. I added some CountDownLatches in my code to avoid timing issues.

@rathnaum
Copy link

I am facing the same issue. I guess connectLatch is set to 1 in the Client and probably increasing it to 2 or 3 might solve the issue.

@marci4 marci4 self-assigned this Apr 15, 2017
marci4 added a commit to marci4/Java-WebSocket-Dev that referenced this issue Apr 15, 2017
Fix for: Client blocking connect and close methods return too soon TooTallNate#302
@marci4
Copy link
Collaborator

marci4 commented Apr 15, 2017

Test code:

public class Test {

	public static void main(String[] args) throws URISyntaxException, InterruptedException {
		WebSocketImpl.DEBUG = false;
		while (true) {
			TestClient client = new TestClient();
			client.connectBlocking();
			client.send();
			client.closeBlocking();
			client.send();
		}
	}

	static class TestClient extends WebSocketClient {

		public TestClient() throws URISyntaxException {
			super( new URI( "ws://localhost:8887" ), new Draft_17() );
		}
		public boolean connect = false;

		public boolean closed = false;

		@Override
		public void onOpen( ServerHandshake handshakedata ) {
			int i = Integer.MIN_VALUE;
			while(i != Integer.MAX_VALUE) {
				i++;
			}
			connect = true;
		}

		@Override
		public void onMessage( String message ) {

		}

		@Override
		public void onClose( int code, String reason, boolean remote ) {
			int i = Integer.MIN_VALUE;
			while(i != Integer.MAX_VALUE) {
				i++;
			}
			closed = true;
		}

		@Override
		public void onError( Exception ex ) {

		}

		public void send() {
			if (connect  && !closed) {
				send( "success" );
			} else if (closed) {
				System.out.println("closed");
			} else {
				throw new RuntimeException(  );
			}
		}
	}
}

@marci4
Copy link
Collaborator

marci4 commented Apr 15, 2017

This issue should be fixed with the pull request #455

Greetings
marci4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants