You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/08/14 05:13:50 UTC

(camel) branch camel-4.10.x updated: CAMEL-22174 Connection is not actually closed due to lease-tracking in smbj (#18892)

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

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


The following commit(s) were added to refs/heads/camel-4.10.x by this push:
     new 1aeabfbec88 CAMEL-22174 Connection is not actually closed due to lease-tracking in smbj (#18892)
1aeabfbec88 is described below

commit 1aeabfbec889ed9da3622342cf9854881e743251
Author: Mark Snijder <sn...@gmail.com>
AuthorDate: Thu Aug 14 07:09:09 2025 +0200

    CAMEL-22174 Connection is not actually closed due to lease-tracking in smbj (#18892)
    
    * CAMEL-22174 Connection is not actually closed due to lease-tracking in SMBJ and creating 1 extra lease with `prePollCheck` which is never released, and therefore it will never gracefully close.
    
    * CAMEL-22174 Improved certainty that we have no partial connections hanging around
    
    We have SMBClient object which always should only have 1 connection because the hostname and port are set in the SmbConfiguration. Therefore, if we ensure that we never call connect when a connection still exists in the SMBClient we cannot run into issues with the connection not closing properly because of the lease counter being wrong.
    
    ---------
    
    Co-authored-by: Mark Snijder <cl...@gvb.nl>
---
 .../apache/camel/component/smb/SmbOperations.java  |  31 ++++--
 .../camel/component/smb/SmbDisconnectIT.java       | 116 +++++++++++++++++++++
 2 files changed, 137 insertions(+), 10 deletions(-)

diff --git a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
index 9d466d8a92c..2a029e29bf3 100644
--- a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
+++ b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
@@ -65,6 +65,7 @@ public class SmbOperations implements SmbFileOperations {
     private Session session;
     private DiskShare share;
     private SMBClient smbClient;
+    private Connection connection;
 
     public SmbOperations(SmbConfiguration configuration) {
         this.configuration = configuration;
@@ -80,12 +81,15 @@ public class SmbOperations implements SmbFileOperations {
 
     protected void connectIfNecessary() {
         try {
-            Connection connection = smbClient.connect(configuration.getHostname(), configuration.getPort());
-
-            if (!loggedIn || !isConnected()) {
+            if (!isConnected()) {
                 LOG.debug("Not already connected/logged in. Connecting to: {}:{}", configuration.getHostname(),
                         configuration.getPort());
 
+                // Clean up any existing partial connections
+                disconnect();
+
+                // Establish fresh connections
+                connection = smbClient.connect(configuration.getHostname(), configuration.getPort());
                 AuthenticationContext ac = new AuthenticationContext(
                         configuration.getUsername(),
                         configuration.getPassword().toCharArray(),
@@ -108,10 +112,10 @@ public class SmbOperations implements SmbFileOperations {
 
     @Override
     public boolean isConnected() throws GenericFileOperationFailedException {
-        if (share != null) {
-            return share.isConnected();
-        }
-        return false;
+        return loggedIn &&
+                connection != null && connection.isConnected() &&
+                session != null &&
+                share != null && share.isConnected();
     }
 
     @Override
@@ -128,17 +132,24 @@ public class SmbOperations implements SmbFileOperations {
         if (session != null) {
             try {
                 session.close();
-                session.getConnection().close();
+            } catch (Exception e) {
+                // ignore
+            }
+            session = null;
+        }
+        if (connection != null) {
+            try {
+                connection.close();
             } catch (TransportException t) {
                 try {
-                    session.getConnection().close(true);
+                    connection.close(true);
                 } catch (IOException e) {
                     // ignore
                 }
             } catch (Exception e) {
                 // ignore
             }
-            session = null;
+            connection = null;
         }
     }
 
diff --git a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbDisconnectIT.java b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbDisconnectIT.java
new file mode 100644
index 00000000000..6ed5a8aa16b
--- /dev/null
+++ b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbDisconnectIT.java
@@ -0,0 +1,116 @@
+/*
+ * 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.smb;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.net.SocketFactory;
+
+import com.hierynomus.smbj.SmbConfig;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SmbDisconnectIT extends SmbServerTestSupport {
+
+    public static final String SOCKET_TRACKING_SMB_CONFIG = "socketTrackingSmbConfig";
+
+    protected String getSmbUrl() {
+        return String.format(
+                "smb:%s/%s/disconnect?username=%s&password=%s&smbConfig=#%s&disconnect=true&initialDelay=5000",
+                service.address(), service.shareName(), service.userName(), service.password(), SOCKET_TRACKING_SMB_CONFIG);
+    }
+
+    @Test
+    public void testProducerDisconnects() {
+        TrackingSocketFactory socketFactory = createAndBindSocketTrackingSmbConfig();
+        sendFile(getSmbUrl(), "Hello World", "hello.txt");
+
+        assertEquals("Hello World", new String(copyFileContentFromContainer("/data/rw/disconnect/hello.txt")));
+        assertSocketsAreClosed(socketFactory, 1, "There should be one tracked and closed socket (producer)");
+    }
+
+    @Test
+    public void testConsumerDisconnects() {
+        TrackingSocketFactory socketFactory = createAndBindSocketTrackingSmbConfig();
+        sendFile(getSmbUrl(), "Hello World", "hello.txt");
+        String out = consumer.receiveBody(getSmbUrl(), 5000L, String.class);
+
+        assertEquals("Hello World", out);
+        assertSocketsAreClosed(socketFactory, 2, "There should be two tracked and closed sockets (producer and consumer)");
+    }
+
+    private TrackingSocketFactory createAndBindSocketTrackingSmbConfig() {
+        TrackingSocketFactory socketFactory = new TrackingSocketFactory();
+        context.getRegistry().bind(SOCKET_TRACKING_SMB_CONFIG, SmbConfig.builder()
+                .withSocketFactory(socketFactory)
+                .build());
+        return socketFactory;
+    }
+
+    private void assertSocketsAreClosed(TrackingSocketFactory socketFactory, int expectedAmount, String message) {
+        assertEquals(expectedAmount, socketFactory.getTrackedSockets().size(), message);
+        socketFactory.getTrackedSockets().forEach(socket -> {
+            assertTrue(socket.isClosed(), "Socket should be closed after disconnect");
+        });
+    }
+
+    /**
+     * SocketFactory that tracks created sockets for connection/disconnection testing.
+     */
+    private static class TrackingSocketFactory extends SocketFactory {
+        private final SocketFactory delegate = SocketFactory.getDefault();
+        private final List<Socket> trackedSockets = new ArrayList<>();
+
+        public List<Socket> getTrackedSockets() {
+            return trackedSockets;
+        }
+
+        @Override
+        public Socket createSocket(String host, int port) throws IOException {
+            Socket socket = delegate.createSocket(host, port);
+            trackedSockets.add(socket);
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
+            Socket socket = delegate.createSocket(host, port, localHost, localPort);
+            trackedSockets.add(socket);
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(InetAddress host, int port) throws IOException {
+            Socket socket = delegate.createSocket(host, port);
+            trackedSockets.add(socket);
+            return socket;
+        }
+
+        @Override
+        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
+            Socket socket = delegate.createSocket(address, port, localAddress, localPort);
+            trackedSockets.add(socket);
+            return socket;
+        }
+    }
+}