Skip to content

Commit 39e640c

Browse files
authored
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
1 parent 5b95429 commit 39e640c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ViteWebsocketConnection implements Listener {
4242

4343
private final Consumer<String> onMessage;
4444
private final Runnable onClose;
45-
private final CompletableFuture<WebSocket> clientWebsocket;
45+
private final CompletableFuture<WebSocket> clientWebsocket = new CompletableFuture<>();
4646
private final List<CharSequence> parts = new ArrayList<>();
4747

4848
private static Logger getLogger() {
@@ -72,15 +72,20 @@ public ViteWebsocketConnection(int port, String path, String subProtocol,
7272
this.onClose = onClose;
7373
String wsHost = ViteHandler.DEV_SERVER_HOST.replace("http://", "ws://");
7474
URI uri = URI.create(wsHost + ":" + port + path);
75-
clientWebsocket = HttpClient.newHttpClient().newWebSocketBuilder()
75+
HttpClient.newHttpClient().newWebSocketBuilder()
7676
.subprotocols(subProtocol).buildAsync(uri, this)
7777
.whenComplete(((webSocket, failure) -> {
7878
if (failure == null) {
7979
getLogger().debug(
8080
"Connection to {} using the {} protocol established",
8181
uri, webSocket.getSubprotocol());
82+
if (clientWebsocket.complete(webSocket)) {
83+
getLogger().trace(
84+
"Websocket future completed in client build completion");
85+
}
8286
} else {
8387
getLogger().debug("Failed to connect to {}", uri);
88+
clientWebsocket.completeExceptionally(failure);
8489
onConnectionFailure.accept(failure);
8590
}
8691
}));
@@ -90,6 +95,9 @@ public ViteWebsocketConnection(int port, String path, String subProtocol,
9095
public void onOpen(WebSocket webSocket) {
9196
getLogger().debug("Connected using the {} protocol",
9297
webSocket.getSubprotocol());
98+
if (clientWebsocket.complete(webSocket)) {
99+
getLogger().trace("Websocket future completed in onOpen");
100+
}
93101
Listener.super.onOpen(webSocket);
94102
}
95103

0 commit comments

Comments
 (0)