From ad8d8367b9060c488f2db838a800dbff5d2442e2 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 8 Apr 2019 18:11:09 -0400 Subject: [PATCH] Fix race condition around `sharedJschSession` https://build.spring.io/browse/INT-MASTER-1481 The `SftpSession.connect()` may lead to race condition when we try to open the `channel`, but `JschSession` is closed already. It may happen in cases when we have `DefaultSftpSessionFactory` configured for the `isSharedSession` and that shared session may be closed by another thread before we reach the `channel.connect()`, because we already have left the `this.sharedSessionLock` blocking path **Cherry-pick to 5.1.x** --- .../sftp/session/DefaultSftpSessionFactory.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java index 0a9e5b6464..69cf2ea4fb 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java @@ -353,6 +353,7 @@ public class DefaultSftpSessionFactory implements SessionFactory, Share "either a password or a private key is required"); try { JSchSessionWrapper jschSession; + SftpSession sftpSession; if (this.isSharedSession) { this.sharedSessionLock.readLock().lock(); try { @@ -375,17 +376,19 @@ public class DefaultSftpSessionFactory implements SessionFactory, Share this.sharedSessionLock.writeLock().unlock(); } } + jschSession = this.sharedJschSession; + sftpSession = new SftpSession(jschSession); + sftpSession.connect(); } finally { this.sharedSessionLock.readLock().unlock(); } - jschSession = this.sharedJschSession; } else { jschSession = new JSchSessionWrapper(initJschSession()); + sftpSession = new SftpSession(jschSession); + sftpSession.connect(); } - SftpSession sftpSession = new SftpSession(jschSession); - sftpSession.connect(); jschSession.addChannel(); return sftpSession; }