davsclaus commented on code in PR #27904:
URL: https://github.com/apache/camel/pull/27904#discussion_r4286923231


##########
components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java:
##########
@@ -182,22 +236,41 @@ protected WebSocket 
getWebSocket(java.util.function.Consumer<WebSocket> setupCal
             client.connect(connectOptions).onComplete(result -> {
                 if (result.succeeded()) {
                     WebSocket ws = result.result();
+                    if (future.isDone()) {
+                        // the caller gave up waiting (see below): nobody 
would use or close this WebSocket
+                        ws.close();
+                        return;
+                    }
                     LOG.info("Connected to WebSocket on {}", 
ws.remoteAddress());
                     if (setupCallback != null) {
                         try {
-                            setupCallback.accept(ws);
+                            setupCallback.accept(ws, false);
                         } catch (Exception e) {
+                            ws.close();
                             future.completeExceptionally(e);
                             return;
                         }
                     }
-                    future.complete(ws);
+                    if (!future.complete(ws)) {
+                        ws.close();
+                    }
                 } else {
-                    webSocket = null;
                     future.completeExceptionally(result.cause());
                 }
             });
-            webSocket = future.get(resolvedClientOptions.getConnectTimeout(), 
TimeUnit.MILLISECONDS);
+            try {
+                webSocket = 
future.get(resolvedClientOptions.getConnectTimeout(), TimeUnit.MILLISECONDS);
+            } catch (Exception e) {
+                webSocket = null;
+                // a connect that completes from now on closes its WebSocket
+                future.cancel(false);

Review Comment:
   _Claude Code on behalf of @davsclaus_
   
   Good catch, the window is real. This PR is a straight cherry-pick and main 
has the same code, so it is fixed on main first in #27911 
([CAMEL-25693](https://issues.apache.org/jira/browse/CAMEL-25693)): when 
`cancel(false)` comes too late and the connect succeeded, the WebSocket the 
future holds is closed. It will be backported to 4.22.x after it is merged.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to