Jubar,

That's great news that you have a fix for your use case!   Do you plan on
submitting a PR?

I have some more testing to do, but I'm seeing some differing results
depending on how long the connection is broken for - something like this
seems to be helping though :

-        return share.list(path,
searchPattern).toArray(FileIdBothDirectoryInformation[]::new);
+        FileIdBothDirectoryInformation[] result;
+        try {
+            result = share.list(path,
searchPattern).toArray(FileIdBothDirectoryInformation[]::new);
+        } catch (SMBRuntimeException smbre) {
+            loggedIn = false;
+            throw smbre;
+        }
+        return result;
     }

I'll do some more testing today.



On Tue, Jun 17, 2025 at 7:35 AM j_b_s34 <j_b_...@proton.me.invalid> wrote:

> Hi,
>
> I'm running 10 minute intervals between the polls and have disconnect=true
> configured so the disconnect() method is called in my case.
> I tested my route today with the following changes and it works pretty
> well.
>
> 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 333d28b0092..ec4f55c72d0 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
> @@ -28,8 +28,10 @@ import
> com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
>  import com.hierynomus.mssmb2.SMB2CreateDisposition;
>  import com.hierynomus.mssmb2.SMB2CreateOptions;
>  import com.hierynomus.mssmb2.SMB2ShareAccess;
> +import com.hierynomus.protocol.transport.TransportException;
>  import com.hierynomus.smbj.SMBClient;
>  import com.hierynomus.smbj.auth.AuthenticationContext;
> +import com.hierynomus.smbj.common.SMBRuntimeException;
>  import com.hierynomus.smbj.connection.Connection;
>  import com.hierynomus.smbj.session.Session;
>  import com.hierynomus.smbj.share.DiskShare;
> @@ -88,7 +90,19 @@ public class SmbOperations implements SmbFileOperations
> {
>                          configuration.getUsername(),
>                          configuration.getPassword().toCharArray(),
>                          configuration.getDomain());
> -                session = connection.authenticate(ac);
> +                try {
> +                    session = connection.authenticate(ac);
> +                } catch (SMBRuntimeException e) {
> +                    if (e.getCause() instanceof TransportException) {
> +                        // discard broken connection
> +                        connection.close(true);
> +                        // get a new connection
> +                        connection =
> smbClient.connect(configuration.getHostname(), configuration.getPort());
> +                        session = connection.authenticate(ac);
> +                    } else {
> +                        throw e;
> +                    }
> +                }
>
>                  share = (DiskShare)
> session.connectShare(configuration.getShareName());
>
> @@ -126,6 +140,12 @@ public class SmbOperations implements
> SmbFileOperations {
>          if (session != null) {
>              try {
>                  session.close();
> +            } catch (TransportException t) {
> +                try {
> +                    session.getConnection().close(true);
> +                } catch (IOException e) {
> +                    // ignore
> +                }
>              } catch (Exception e) {
>                  // ignore
>              }
>
> I also thought about sending a SMB2Echo request in isConnected() to verify
> that the socket is alive but i don't know if that tanks the performance too
> much.
>
> Greetings,
> jubar
>
> Sent with Proton Mail secure email.
>
> On Tuesday, June 17th, 2025 at 00:15, Tom Cunningham
> <tcunn...@redhat.com.INVALID> wrote:
>
> > I gave this a try a couple of times and I don't see disconnect() called
> at
> > all in this situation - I do not think that is the issue here. I
> > think the Share needs to be refreshed at some point after the timeout,
> I'll
> > try to look for a way to do that.
> >
> >
> >
> > On Mon, Jun 16, 2025 at 7:47 AM j_b_s34 j_b_...@proton.me.invalid wrote:
> >
> > > Would catching the exception from session.close() and then force
> closing
> > > the connection fix the issue?
> > > Example:
> > > ...
> > > if (session != null) {
> > > try {
> > > session.close();
> > > } catch (Exception e) {
> > > if (e.getCause() instanceof TransportException) {
> > > try {
> > > session.getConnection().close(true);
> > > } catch (IOException ignored) {
> > > //ignore
> > > }
> > > }
> > > }
> > > session = null;
> > > }
> > > ...
> > >
> > > Or is it cleaner to close the SmbClient?
> > >
> > > Sent with Proton Mail secure email.
> > >
> > > On Monday, June 16th, 2025 at 10:37, ski n raymondmees...@gmail.com
> > > wrote:
> > >
> > > > I think this is a bug in the underlying library smbj:
> > > >
> > > > https://github.com/hierynomus/smbj/issues/864
> > > >
> > > > And this error was also discussed on the Camel forum:
> > >
> > >
> https://camel.zulipchat.com/#narrow/channel/257298-camel/topic/SMB.20Componnent.20how.20the.20connection.20alive.3F/with/507977443
> > >
> > > > As I can see the smbj isn't properly maintained. It has 177 open
> issues,
> > > > and no commits for 5 months. For now, I am using my custom Samba
> > > > component
> > > > based jcifs:
> > > >
> > > > https://github.com/assimbly/custom-components/tree/develop/smb
> > > >
> > > > Raymond
> > > >
> > > > On Mon, Jun 16, 2025 at 9:49 AM j_b_s34 j_b_...@proton.me.invalid
> wrote:
> > > >
> > > > > Hi everyone,
> > > > >
> > > > > I'm using a simple route to consume files from an SMB share and
> store
> > > > > them
> > > > > in a database.
> > > > > When the remote server becomes unreachable, the consumer keeps
> trying
> > > > > to
> > > > > reconnect but seems to reuse a broken connection that wasn’t
> properly
> > > > > closed.
> > > > > The only fix is restarting the whole route.
> > > > > It looks like disconnect() in SmbOperations doesn't fully close the
> > > > > connection after an exception:
> > >
> > >
> https://github.com/apache/camel/blob/a2fe5b8057d0c990da048a49b380b705ad2237c6/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java#L116-L134
> > >
> > >
> https://github.com/apache/camel/blob/a2fe5b8057d0c990da048a49b380b705ad2237c6/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java#L116-L134
> > >
> > > > > Kind regards,
> > > > > jubar.
> > > > >
> > > > > StackTrace:
> > > > > com.hierynomus.smbj.common.SMBRuntimeException:
> > > > > com.hierynomus.protocol.transport.TransportException:
> > > > > java.net.SocketException: Broken pipe
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.SMBSessionBuilder.establish(SMBSessionBuilder.java:124)
> > >
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.Connection.authenticate(Connection.java:206)
> > >
> > > > > at
> > >
> > >
> org.apache.camel.component.smb.SmbOperations.connectIfNecessary(SmbOperations.java:91)
> > >
> > > > > at
> > >
> > >
> org.apache.camel.component.smb.SmbConsumer.prePollCheck(SmbConsumer.java:207)
> > >
> > > > > at
> > >
> > >
> org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:129)
> > >
> > > > > at
> > >
> > >
> org.apache.camel.support.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:208)
> > >
> > > > > at
> > >
> > >
> org.apache.camel.support.ScheduledPollConsumer.run(ScheduledPollConsumer.java:119)
> > >
> > > > > at
> > > > >
> java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown
> > > > > Source)
> > > > > at java.base/java.util.concurrent.FutureTask.runAndReset(Unknown
> > > > > Source)
> > > > > at
> > >
> > >
> java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown
> > >
> > > > > Source)
> > > > > at
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown
> > > > > Source)
> > > > > at
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown
> > > > > Source)
> > > > > at io.opentelemetry.context.Context.lambda$wrap$1(Context.java:241)
> > > > > at java.base/java.lang.Thread.run(Unknown Source)
> > > > > Caused by: com.hierynomus.protocol.transport.TransportException:
> > > > > java.net.SocketException: Broken pipe
> > > > > at
> > >
> > >
> com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.write(DirectTcpTransport.java:86)
> > >
> > > > > at
> com.hierynomus.smbj.connection.Connection.send(Connection.java:242)
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.Connection.sendAndReceive(Connection.java:247)
> > >
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.SMBSessionBuilder.initiateSessionSetup(SMBSessionBuilder.java:217)
> > >
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.SMBSessionBuilder.setupSession(SMBSessionBuilder.java:136)
> > >
> > > > > at
> > >
> > >
> com.hierynomus.smbj.connection.SMBSessionBuilder.establish(SMBSessionBuilder.java:119)
> > >
> > > > > ... 13 more
> > > > > Caused by: java.net.SocketException: Broken pipe
> > > > > at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method)
> > > > > at java.base/sun.nio.ch.SocketDispatcher.write(Unknown Source)
> > > > > at java.base/sun.nio.ch.NioSocketImpl.tryWrite(Unknown Source)
> > > > > at java.base/sun.nio.ch.NioSocketImpl.implWrite(Unknown Source)
> > > > > at java.base/sun.nio.ch.NioSocketImpl.write(Unknown Source)
> > > > > at java.base/sun.nio.ch.NioSocketImpl$2.write(Unknown Source)
> > > > > at java.base/java.net.Socket$SocketOutputStream.write(Unknown
> Source)
> > > > > at java.base/java.io.BufferedOutputStream.flushBuffer(Unknown
> Source)
> > > > > at java.base/java.io.BufferedOutputStream.implWrite(Unknown
> Source)
> > > > > at java.base/java.io.BufferedOutputStream.write(Unknown Source)
> > > > > at
> > >
> > >
> com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.writeDirectTcpPacketHeader(DirectTcpTransport.java:163)
> > >
> > > > > at
> > >
> > >
> com.hierynomus.smbj.transport.tcp.direct.DirectTcpTransport.write(DirectTcpTransport.java:81)
> > >
> > > > > ... 18 more
> > > > >
> > > > > Sent with Proton Mail secure email.
>
>

Reply via email to