Skip to content

Commit ad8d836

Browse files
artembilangaryrussell
authored andcommitted
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**
1 parent bfebbbb commit ad8d836

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/DefaultSftpSessionFactory.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public SftpSession getSession() {
353353
"either a password or a private key is required");
354354
try {
355355
JSchSessionWrapper jschSession;
356+
SftpSession sftpSession;
356357
if (this.isSharedSession) {
357358
this.sharedSessionLock.readLock().lock();
358359
try {
@@ -375,17 +376,19 @@ public SftpSession getSession() {
375376
this.sharedSessionLock.writeLock().unlock();
376377
}
377378
}
379+
jschSession = this.sharedJschSession;
380+
sftpSession = new SftpSession(jschSession);
381+
sftpSession.connect();
378382
}
379383
finally {
380384
this.sharedSessionLock.readLock().unlock();
381385
}
382-
jschSession = this.sharedJschSession;
383386
}
384387
else {
385388
jschSession = new JSchSessionWrapper(initJschSession());
389+
sftpSession = new SftpSession(jschSession);
390+
sftpSession.connect();
386391
}
387-
SftpSession sftpSession = new SftpSession(jschSession);
388-
sftpSession.connect();
389392
jschSession.addChannel();
390393
return sftpSession;
391394
}

0 commit comments

Comments
 (0)