From 8aef55f061f196196262e067d9d0591977cd45ba 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 17ec08074a..fc39643ccb 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 @@ -355,6 +355,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 { @@ -377,17 +378,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; }