Skip to content

Commit 8aef55f

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 818be4c commit 8aef55f

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
@@ -355,6 +355,7 @@ public SftpSession getSession() {
355355
"either a password or a private key is required");
356356
try {
357357
JSchSessionWrapper jschSession;
358+
SftpSession sftpSession;
358359
if (this.isSharedSession) {
359360
this.sharedSessionLock.readLock().lock();
360361
try {
@@ -377,17 +378,19 @@ public SftpSession getSession() {
377378
this.sharedSessionLock.writeLock().unlock();
378379
}
379380
}
381+
jschSession = this.sharedJschSession;
382+
sftpSession = new SftpSession(jschSession);
383+
sftpSession.connect();
380384
}
381385
finally {
382386
this.sharedSessionLock.readLock().unlock();
383387
}
384-
jschSession = this.sharedJschSession;
385388
}
386389
else {
387390
jschSession = new JSchSessionWrapper(initJschSession());
391+
sftpSession = new SftpSession(jschSession);
392+
sftpSession.connect();
388393
}
389-
SftpSession sftpSession = new SftpSession(jschSession);
390-
sftpSession.connect();
391394
jschSession.addChannel();
392395
return sftpSession;
393396
}

0 commit comments

Comments
 (0)