Skip to content

Commit a31ca5e

Browse files
committed
Fix incorrect client WebSocket upgrade handshake timeout cancellation.
Motivation: When the HTTP client connection ugprades to a WebSocket it sets a timeout for the WebSocket handshake, when the upgrade is succesfull it cancels the timeout timer. The current cancellation check does not account for the initial timer ID that is 0. Changes: Cancel correctly the timeout timer.
1 parent 27e488f commit a31ca5e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ synchronized void toWebSocket(
969969
}
970970

971971
long timer;
972-
if (handshakeTimeout > 0L) {
972+
if (handshakeTimeout > -1L) {
973973
timer = vertx.setTimer(handshakeTimeout, id -> closeInternal());
974974
} else {
975975
timer = -1;
@@ -1000,7 +1000,7 @@ synchronized void toWebSocket(
10001000
WebSocketHandshakeInboundHandler handshakeInboundHandler = new WebSocketHandshakeInboundHandler(handshaker, upgrade);
10011001
p.addBefore("handler", "handshakeCompleter", handshakeInboundHandler);
10021002
upgrade.addListener((GenericFutureListener<io.netty.util.concurrent.Future<HttpHeaders>>) future -> {
1003-
if (timer > 0L) {
1003+
if (timer > -1L) {
10041004
vertx.cancelTimer(timer);
10051005
}
10061006
if (future.isSuccess()) {

vertx-core/src/test/java/io/vertx/tests/http/WebSocketTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ public List<Throwable> getReceivedExceptions() {
17221722
}
17231723

17241724
@Test
1725-
public void testHandshakeTimeout() throws Exception {
1725+
public void testHandshakeTimeoutFires() throws Exception {
17261726
NetServer server = vertx.createNetServer()
17271727
.connectHandler(so -> {
17281728

@@ -1746,6 +1746,32 @@ public void testHandshakeTimeout() throws Exception {
17461746
}
17471747
}
17481748

1749+
@Test
1750+
public void testHandshakeTimeoutDoesNotFire() throws Exception {
1751+
server = vertx.createHttpServer()
1752+
.webSocketHandler(ws -> {
1753+
1754+
});
1755+
server
1756+
.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST)
1757+
.await();
1758+
client = vertx.createWebSocketClient(new WebSocketClientOptions().setConnectTimeout(1000));
1759+
WebSocketConnectOptions options = new WebSocketConnectOptions()
1760+
.setPort(DEFAULT_HTTP_PORT)
1761+
.setHost(DEFAULT_HTTP_HOST)
1762+
.setURI("/")
1763+
.setTimeout(1000);
1764+
client.connect(options).onComplete(onSuccess(ws -> {
1765+
AtomicBoolean closed = new AtomicBoolean();
1766+
ws.closeHandler(v -> closed.set(true));
1767+
vertx.setTimer(1100, id -> {
1768+
assertFalse(closed.get());
1769+
testComplete();
1770+
});
1771+
}));
1772+
await();
1773+
}
1774+
17491775
private void connectUntilWebSocketReject(WebSocketClient client, int count, Handler<AsyncResult<Void>> doneHandler) {
17501776
vertx.runOnContext(v -> {
17511777
client.connect(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path").onComplete(ar -> {

0 commit comments

Comments
 (0)