This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 38027af29d0e [backport camel-4.22.x] CAMEL-25693: 
camel-vertx-websocket - close a WebSocket that connects just after the connect 
timeout (#27965)
38027af29d0e is described below

commit 38027af29d0e75b858fa334096d10062be074580
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Oct 16 21:16:53 2026 +0200

    [backport camel-4.22.x] CAMEL-25693: camel-vertx-websocket - close a 
WebSocket that connects just after the connect timeout (#27965)
---
 .../vertx/websocket/VertxWebsocketEndpoint.java    | 15 ++++-
 .../VertxWebsocketAbandonConnectTest.java          | 78 ++++++++++++++++++++++
 2 files changed, 91 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
index 4a782648de1c..97e8a0634587 100644
--- 
a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
@@ -262,8 +262,7 @@ public class VertxWebsocketEndpoint extends DefaultEndpoint 
implements EndpointS
                 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);
+                abandonConnect(future);
                 throw e;
             }
         } else {
@@ -275,6 +274,18 @@ public class VertxWebsocketEndpoint extends 
DefaultEndpoint implements EndpointS
         return webSocket;
     }
 
+    /**
+     * Gives up on a connect the caller stopped waiting for. A connect that 
completes from now on closes its own
+     * WebSocket. One that completed after the wait timed out but before this 
call is closed here, as nobody will use
+     * it.
+     */
+    static void abandonConnect(CompletableFuture<WebSocket> future) {
+        if (!future.cancel(false) && !future.isCompletedExceptionally()) {
+            // the future completed normally, so join() returns at once
+            future.join().close();
+        }
+    }
+
     protected WebSocket getWebSocket(Exchange exchange) throws Exception {
         return getWebSocket().exceptionHandler(event -> 
exchange.setException(event.getCause()));
     }
diff --git 
a/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketAbandonConnectTest.java
 
b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketAbandonConnectTest.java
new file mode 100644
index 000000000000..aab975c5be4a
--- /dev/null
+++ 
b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketAbandonConnectTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.vertx.websocket;
+
+import java.io.IOException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+import io.vertx.core.http.WebSocket;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A connect the endpoint stopped waiting for must not leave an open WebSocket 
behind (CAMEL-25693).
+ */
+public class VertxWebsocketAbandonConnectTest extends 
VertxWebSocketTestSupport {
+
+    @Test
+    void connectCompletedAfterTheTimeoutIsClosed() throws Exception {
+        WebSocket webSocket = openWebSocketConnection("localhost", 
port.getPort(), "/test", message -> {
+        });
+        assertFalse(webSocket.isClosed());
+
+        // the connect completed between the timed out wait and the cancel
+        CompletableFuture<WebSocket> future = 
CompletableFuture.completedFuture(webSocket);
+        VertxWebsocketEndpoint.abandonConnect(future);
+
+        await().atMost(10, TimeUnit.SECONDS).until(webSocket::isClosed);
+    }
+
+    @Test
+    void pendingConnectIsCancelled() {
+        CompletableFuture<WebSocket> future = new CompletableFuture<>();
+        VertxWebsocketEndpoint.abandonConnect(future);
+
+        // the connect handler sees the cancelled future and closes the 
WebSocket itself
+        assertTrue(future.isCancelled());
+    }
+
+    @Test
+    void failedConnectIsIgnored() {
+        CompletableFuture<WebSocket> future = 
CompletableFuture.failedFuture(new IOException("Connection refused"));
+        VertxWebsocketEndpoint.abandonConnect(future);
+
+        assertTrue(future.isCompletedExceptionally());
+        assertFalse(future.isCancelled());
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                fromF("vertx-websocket:localhost:%d/test", port.getPort())
+                        .log("${body}");
+            }
+        };
+    }
+}

Reply via email to