WebSockets Next: client onClose not called when the close handshake times out ("Invalid status code: 1006")
Describe the bug
When the client closes a connection and the server never answers the close frame (e.g. it is behind a network partition), Vert.x closes the TCP connection after connection-closing-timeout and reports the status 1006. The close handler in BasicWebSocketConnectorImpl then calls new CloseReason(ws.closeStatusCode(), ws.closeReason()), which throws because 1006 is not a valid status code to send:
ERROR [io.quarkus.vertx.core.runtime.VertxCoreRecorder] Uncaught exception received by Vert.x: java.lang.IllegalArgumentException: Invalid status code: 1006As a result onClose is not called, and the rest of the handler is skipped: connectionManager.remove(...) and client.get().close(), so the connection is not released.
Expected behavior
onClose is called (e.g. with CloseReason.ABNORMAL) and the connection is released.
Actual behavior
The exception above is logged, onClose is not called, the connection is not released.
How to Reproduce?
Test for extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ (fails on 3.40.1 with [onClose called] Expecting value to be true but was false and the error above). The server accepts the handshake, then never answers:
package io.quarkus.websockets.next.test.client;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import io.quarkus.test.QuarkusExtensionTest;
import io.quarkus.websockets.next.BasicWebSocketConnector;
import io.quarkus.websockets.next.WebSocketClientConnection;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
public class BasicConnectorCloseHandshakeTimeoutTest {
@RegisterExtension
public static final QuarkusExtensionTest test = new QuarkusExtensionTest()
.withApplicationRoot(root -> {
})
.overrideRuntimeConfigKey("quarkus.websockets-next.client.connection-closing-timeout", "1s");
@Inject
Vertx vertx;
@Inject
BasicWebSocketConnector connector;
@Test
void callsOnCloseWhenTheCloseHandshakeTimesOut() throws Exception {
// accepts the WebSocket handshake, then does not answer anything (as a peer behind a network partition)
NetServer server = vertx.createNetServer()
.connectHandler(socket -> {
Buffer request = Buffer.buffer();
socket.handler(data -> {
request.appendBuffer(data);
String text = request.toString();
if (text.contains("\r\n\r\n")) {
String key = text.lines()
.filter(l -> l.regionMatches(true, 0, "Sec-WebSocket-Key:", 0, 18))
.findFirst().orElseThrow().substring(18).trim();
socket.write("HTTP/1.1 101 Switching Protocols\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: " + accept(key) + "\r\n\r\n");
}
});
})
.listen(0).toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
try {
CountDownLatch closed = new CountDownLatch(1);
AtomicInteger code = new AtomicInteger();
WebSocketClientConnection connection = connector.baseUri("ws://localhost:" + server.actualPort())
.onClose((c, reason) -> {
code.set(reason.getCode());
closed.countDown();
})
.connectAndAwait();
// the server never answers the close frame: Vert.x closes the TCP connection after the closing timeout,
// and reports the status 1006
connection.close().subscribe().with(v -> {
}, t -> {
});
assertThat(closed.await(10, TimeUnit.SECONDS)).as("onClose called").isTrue();
} finally {
server.close().toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
}
}
private static String accept(String key) {
try {
byte[] sha1 = MessageDigest.getInstance("SHA-1")
.digest((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes(StandardCharsets.US_ASCII));
return Base64.getEncoder().encodeToString(sha1);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
}Output of uname -a or ver
Darwin 27.0.0
Output of java -version
openjdk 25.0.2 (Temurin)
Quarkus version or git rev
3.40.1 (same code on main at cd8646d2438)
Build tool (ie. output of mvnw --version or gradlew --version)
Maven 3.9.16
Additional information
Falling back to CloseReason.ABNORMAL when the received code is not valid makes the test pass. Running the cleanup in a finally would also avoid leaking the connection if onClose itself fails.
Source: quarkusio/quarkus