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

davsclaus pushed a commit to branch fix/CAMEL-25693
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a18687b90cd6e68fa8ce86c2ece07d39e4ba816d
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Oct 15 20:42:47 2026 +0200

    CAMEL-25693: camel-vertx-websocket - close a WebSocket that connects just 
after the connect timeout
    
    When the connect finished between the timed out wait and the cancel of the 
connect future, the cancel had no
    effect and nothing closed the WebSocket. Close the WebSocket the future 
completed with in that case.
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../vertx/websocket/VertxWebsocketEndpoint.java    | 14 +++-
 .../VertxWebsocketAbandonConnectTest.java          | 78 ++++++++++++++++++++++
 2 files changed, 90 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 bd5de01d716b..b186e3355647 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
@@ -275,8 +275,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 {
@@ -288,6 +287,17 @@ 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()) {
+            future.join().close();
+        }
+    }
+
     protected WebSocket getWebSocket(Exchange exchange) throws Exception {
         // The handler stays on the WebSocket after the exchange is done. A 
lost connection is not set on the exchange: a
         // closed WebSocket is replaced by a new connection on the next send, 
and a write in flight fails through its result
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