gnodet-bot commented on code in PR #30177:
URL: https://github.com/apache/camel/pull/30177#discussion_r4585172338
##########
components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java:
##########
@@ -862,6 +871,92 @@ public void disconnect() throws
GenericFileOperationFailedException {
}
}
+ /**
+ * Disconnects when the producer or the consumer stops. Another thread can
still run an operation that holds the
+ * lock: an upload of the producer, or the poll of the consumer, which the
stop of its scheduler interrupts, but
+ * which then changes back to the directory it started from (stepwise), on
a server that may not answer.
+ * <p>
+ * As long as the shutdown of Camel is not forced, this method waits for
that operation, as {@link #disconnect()}
+ * does: it can be a healthy but slow upload, when a producer cache evicts
the producer, when an application stops a
+ * {@code ProducerTemplate}, or during a graceful shutdown, which stops
the producers of a route whose consumer it
+ * stops right away (one that cannot be suspended) while its exchanges are
still in flight.
+ * <p>
+ * Once the shutdown is forced, as its timeout elapsed, the operation is
one that the shutdown gave up waiting for,
+ * such as an upload that hangs as long as the server does not answer
(Apache MINA SSHD waits for an answer up to
+ * its idle timeout, 10 minutes by default). This method then closes the
session without the lock, so that the
+ * operation fails and releases the lock. A connect in progress is waited
for: the connect timeout and the reconnect
+ * attempts bound it, and its next attempt would open a new session.
+ * <p>
+ * With the lock, it disconnects also when not connected: a connect that
failed, such as one whose password the
+ * server rejected, has already started the SSH client, whose threads
{@link #disconnect()} releases.
+ */
+ @Override
+ public void disconnectOnStop() throws GenericFileOperationFailedException {
+ boolean locked = false;
+ boolean interrupted = false;
+ // a stop that began before the shutdown was forced (as above) is
still waiting at that time, so check again
+ while (!locked) {
+ try {
+ locked = lock.tryLock(DISCONNECT_ON_STOP_WAIT_MILLIS,
TimeUnit.MILLISECONDS);
+ if (!locked && !connecting && isForceShutdown()) {
+ break;
+ }
+ } catch (InterruptedException e) {
+ // keep waiting, as lock() would: the shutdown task, which a
shutdown cancels when it times out, is
+ // interrupted before the shutdown is forced; the interrupt is
restored at the end
+ interrupted = true;
+ }
+ }
+ try {
+ if (locked) {
+ try {
+ disconnect();
+ } finally {
+ lock.unlock();
+ }
+ } else {
+ disconnectWithoutLock();
+ }
+ } finally {
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ private boolean isForceShutdown() {
+ return
endpoint.getCamelContext().getShutdownStrategy().isForceShutdown();
+ }
+
+ private void disconnectWithoutLock() {
+ ClientSession current = session;
+ if (current == null || !current.isOpen()) {
+ return;
+ }
+ String server = endpoint.getConfiguration().remoteServerInformation();
+ LOG.warn("The shutdown is forced: disconnecting from {} while another
thread still runs an operation, such as an"
+ + " upload or a download that hangs. The operation fails.",
+ server);
+ // closes the session at once, without waiting for the server: its
SFTP channel closes, and the requests that
+ // wait for an answer of the server fail
+ current.close(true);
+ // then stop the SSH client as disconnect() does, once the operation
released the lock
+ try {
+ if (lock.tryLock(DISCONNECT_ON_STOP_WAIT_MILLIS,
TimeUnit.MILLISECONDS)) {
+ try {
+ disconnect();
+ } finally {
+ lock.unlock();
+ }
+ } else {
+ LOG.debug("The operation of another thread still holds the
lock after the session to {} was closed",
+ server);
+ }
Review Comment:
💡 **Nit (best-effort, acceptable):** When `tryLock` fails here,
`disconnect()` is never called, so the SSH client's NIO2 and timer daemon
threads remain alive until GC collects the client. This mirrors the
`camel-sftp` design and is acceptable as a best-effort forced shutdown — the
session is already closed by `current.close(true)` above. No change needed.
--
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]