davsclaus opened a new pull request, #30596:
URL: https://github.com/apache/camel/pull/30596

   ## What was wrong
   
   [CAMEL-27370](https://issues.apache.org/jira/browse/CAMEL-27370), two 
problems in `SmbOperations.disconnect`:
   
   1. **Stopping when the server goes away takes minutes.** smbj waits up to 
its transact timeout (60 seconds by default, 
`SmbConfig.withTimeout`/`withTransactTimeout`) for each answer: the 
`TreeDisconnect` in `Share.close` and the `Logoff` in `Session.close`. I 
reproduced it by pausing the Samba container (the server stops answering and 
sends no RST). With the default `SmbConfig`, the poll waited 60 s, then the 
disconnect waited 60 s for the share and 60 s for the logoff: `context.stop()` 
took **178 s**.
   2. **A WARN on every stop.** smbj caches directory listings under a 
directory lease (SMB 3). `Connection.close` closes these leased directories 
only after the sessions and shares are closed (`LeaseManager.close`), so each 
logs `WARN Directory - Directory close failed for \\host\share\dir ... 
DiskShare has already been closed`. This happens in smbj's own close order too, 
not only in Camel's.
   
   I checked smbj's timeouts first. Every wait in the close path is bounded by 
`transactTimeout`, but it is one timeout for all requests, so lowering it also 
shortens the timeout of normal listings, reads and writes. smbj has no per-call 
timeout for close, and no way to drop a connection without the graceful close 
(`Connection.close(true)` still closes the leased directories over the network 
first).
   
   ## The fix
   
   - `disconnect()` runs the close in a thread of its own 
(`ExecutorServiceManager.newThread("SmbDisconnect", ...)`, as the sftp 
component does since CAMEL-27001) and waits at most `DISCONNECT_WAIT_MILLIS` (2 
s). A server that answers takes milliseconds. If the server does not answer, it 
logs `WARN Disconnecting from host:port waits for the server, which does not 
answer. The disconnect completes in the background.` The close then finishes in 
the background, still bounded by smbj's timeouts. The next connect uses a new 
`SMBClient`, because smbj's client would otherwise hand out the unanswered 
connection again (it caches connections by host and port while the socket is 
open).
   - Before closing the share, the leased directories are closed with 
`connection.getLeaseManager().close()`, while the share is still open. 
Connection.close then has nothing left to close, so there is no WARN.
   - Failures while closing the share are now ignored like those of the session 
and connection. Before, only an `IOException` was ignored, and an 
`SMBRuntimeException` skipped the session and connection close.
   
   No new option. CAMEL-27369 adds endpoint options for the smbj timeouts 
(`transactTimeout` etc.). The 2 s bound only decides how long the poll or stop 
thread waits, not how long smbj waits.
   
   Not changed: a poll that is running when the server goes away still waits 
the smbj timeout (60 s by default) for its own request. That is the operation 
timeout, configurable with `smbConfig` (or the CAMEL-27369 options).
   
   ## Tests
   
   - `SmbOperationsDisconnectTest` (new, unit, Mockito): the leased directories 
are closed before the share, session and connection (`InOrder`). With a share 
close that does not answer, `disconnect()` returns within the bound and the 
close completes in the background. On the old code, both fail: disconnect took 
60 s, and the lease manager is never closed.
   - `SmbServerGoneIT` (new, its own Samba container, `SmbConfig` timeout 10 s):
     - `stopWhenTheServerDoesNotAnswer` pauses the container and stops the 
context: 2 s with the fix, 20 s on the old code (assertion fails).
     - `stopLogsNoWarnForLeasedDirectories` (`noop=true`, so the lease is kept) 
asserts no WARN on stop. On the old code it fails with the `Directory close 
failed ... DiskShare has already been closed` WARN.
   - Full camel-smb suite with Docker: `mvn verify` in `components/camel-smb`: 
28 unit tests and 88 ITs, all pass. The `Directory close failed` WARN, logged 
40 times in a full IT run before, no longer appears.
   
   Test dependency added: `mockito-core` (test scope).
   
   _Claude Code on behalf of davsclaus_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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