-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Comments
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. |
I can confirm the issue. I added some CountDownLatches in my code to avoid timing issues. |
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. |
Fix for: Client blocking connect and close methods return too soon TooTallNate#302
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( );
}
}
}
} |
This issue should be fixed with the pull request #455 Greetings |
In WebSocketClient, the connect/close latches will count down before the callback is called:
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:
This is a simplified example. Our actual wrapper code has more to it.
The text was updated successfully, but these errors were encountered: