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**
This commit is contained in:
Artem Bilan
2019-04-08 18:11:09 -04:00
committed by Gary Russell
parent bfebbbb848
commit ad8d8367b9

View File

@@ -353,6 +353,7 @@ public class DefaultSftpSessionFactory implements SessionFactory<LsEntry>, 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<LsEntry>, 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;
}