From 39e640c541f83626af060ab9ee40e211ce400990 Mon Sep 17 00:00:00 2001 From: Marco Collovati Date: Tue, 10 Dec 2024 13:11:17 +0100 Subject: [PATCH] fix: complete client websocket future on open (#20587) * fix: complete client websocket future on open Completing the websocket future in onOpen event prevents the connection to hang indefintely when application run on low resources. Fixes #20586 * apply review suggestions --- .../devserver/viteproxy/ViteWebsocketConnection.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java b/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java index 3b14308ced6..cf38b3251cf 100644 --- a/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java +++ b/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java @@ -42,7 +42,7 @@ public class ViteWebsocketConnection implements Listener { private final Consumer onMessage; private final Runnable onClose; - private final CompletableFuture clientWebsocket; + private final CompletableFuture clientWebsocket = new CompletableFuture<>(); private final List parts = new ArrayList<>(); private static Logger getLogger() { @@ -72,15 +72,20 @@ public ViteWebsocketConnection(int port, String path, String subProtocol, this.onClose = onClose; String wsHost = ViteHandler.DEV_SERVER_HOST.replace("http://", "ws://"); URI uri = URI.create(wsHost + ":" + port + path); - clientWebsocket = HttpClient.newHttpClient().newWebSocketBuilder() + HttpClient.newHttpClient().newWebSocketBuilder() .subprotocols(subProtocol).buildAsync(uri, this) .whenComplete(((webSocket, failure) -> { if (failure == null) { getLogger().debug( "Connection to {} using the {} protocol established", uri, webSocket.getSubprotocol()); + if (clientWebsocket.complete(webSocket)) { + getLogger().trace( + "Websocket future completed in client build completion"); + } } else { getLogger().debug("Failed to connect to {}", uri); + clientWebsocket.completeExceptionally(failure); onConnectionFailure.accept(failure); } })); @@ -90,6 +95,9 @@ public ViteWebsocketConnection(int port, String path, String subProtocol, public void onOpen(WebSocket webSocket) { getLogger().debug("Connected using the {} protocol", webSocket.getSubprotocol()); + if (clientWebsocket.complete(webSocket)) { + getLogger().trace("Websocket future completed in onOpen"); + } Listener.super.onOpen(webSocket); }