gnodet-bot commented on code in PR #30596:
URL: https://github.com/apache/camel/pull/30596#discussion_r4657559902


##########
components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java:
##########
@@ -128,21 +144,66 @@ public boolean isConnected() throws 
GenericFileOperationFailedException {
     @Override
     public void disconnect() throws GenericFileOperationFailedException {
         loggedIn = false;
+        DiskShare currentShare = share;
+        Session currentSession = session;
+        Connection currentConnection = connection;
+        share = null;
+        session = null;
+        connection = null;
+        if (currentShare == null && currentSession == null && 
currentConnection == null) {
+            return;
+        }
+        if (endpoint == null) {
+            close(currentConnection, currentSession, currentShare);
+            return;
+        }
+        Thread disconnector = 
endpoint.getCamelContext().getExecutorServiceManager()
+                .newThread("SmbDisconnect", () -> close(currentConnection, 
currentSession, currentShare));
+        disconnector.start();
+        try {
+            disconnector.join(DISCONNECT_WAIT_MILLIS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
+        if (disconnector.isAlive()) {
+            LOG.warn("Disconnecting from {}:{} waits for the server, which 
does not answer."
+                     + " The disconnect completes in the background.",
+                    configuration.getHostname(), configuration.getPort());
+            // the SMB client hands out its open connection again to the next 
connect, until the connection is closed
+            if (smbClient != null) {
+                smbClient = createSmbClient();
+            }
+        }
+    }
+
+    /**
+     * Closes the share, the session and the connection, in this order, 
ignoring failures.
+     */
+    static void close(Connection connection, Session session, DiskShare share) 
{

Review Comment:
   💡 **Javadoc: mention the threading contract and exception-swallowing intent**
   
   This method is designed to be called from a background thread (the 
`"SmbDisconnect"` thread) and intentionally swallows all exceptions. The 
current Javadoc says _"ignoring failures"_ but doesn't say why. Consider:
   
   ```suggestion
       /**
        * Closes the share, the session and the connection, in this order, 
ignoring failures.
        * Designed to be called from a background thread; all exceptions are 
swallowed
        * deliberately so that a misbehaving server cannot prevent the 
remaining resources
        * from being closed.
        */
   ```



##########
components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java:
##########
@@ -128,21 +144,66 @@ public boolean isConnected() throws 
GenericFileOperationFailedException {
     @Override
     public void disconnect() throws GenericFileOperationFailedException {
         loggedIn = false;
+        DiskShare currentShare = share;
+        Session currentSession = session;
+        Connection currentConnection = connection;
+        share = null;
+        session = null;
+        connection = null;
+        if (currentShare == null && currentSession == null && 
currentConnection == null) {
+            return;
+        }
+        if (endpoint == null) {
+            close(currentConnection, currentSession, currentShare);
+            return;
+        }
+        Thread disconnector = 
endpoint.getCamelContext().getExecutorServiceManager()
+                .newThread("SmbDisconnect", () -> close(currentConnection, 
currentSession, currentShare));
+        disconnector.start();
+        try {
+            disconnector.join(DISCONNECT_WAIT_MILLIS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        }
+        if (disconnector.isAlive()) {
+            LOG.warn("Disconnecting from {}:{} waits for the server, which 
does not answer."
+                     + " The disconnect completes in the background.",
+                    configuration.getHostname(), configuration.getPort());
+            // the SMB client hands out its open connection again to the next 
connect, until the connection is closed
+            if (smbClient != null) {
+                smbClient = createSmbClient();

Review Comment:
   🐛 **Resource leak: old `SMBClient` not closed before reassignment**
   
   When the disconnect times out and `disconnector.isAlive()` is true, the old 
`smbClient` is overwritten by `createSmbClient()` without first calling 
`close()` on it. The old client's underlying connection is still live (which is 
precisely why the reconnect path needs a fresh client), but `SMBClient` holds 
resources that should be released — otherwise the abandoned client accumulates 
until GC.
   
   Consider capturing the old reference first:
   ```suggestion
                   SMBClient old = smbClient;
                   smbClient = createSmbClient();
                   old.close();
   ```
   If `close()` on a half-connected client can itself block, wrap it in a 
try/catch and log at DEBUG.



-- 
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