-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Optimize DefaultSftpSessionFactory #2896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize DefaultSftpSessionFactory #2896
Conversation
Related to https://build.spring.io/browse/INT-MASTERSPRING40-677/ Doesn't look like `DefaultSftpSessionFactory.getSession()` needs locking around `sharedJschSession` * change the logic in the `getSession()` to store a `sharedJschSession` into the local variable and if it is `null` or not connected, create a new `JSchSessionWrapper`, connect it and store to the `sharedJschSession` back into the `sharedJschSession` property if `this.isSharedSession`. This way we always deal with `sharedJschSession` anyway if it is valid or create a new fresh one if that is invalid. Without locking we always get an actual state of the `sharedJschSession` and don't fall into the race condition when `sharedJschSession` is invalid, but we can't connect to the SFTP channel from the `sftpSession.connect()`
I won't insist for this change, but read/write locking brings us a race condition when we fail in the At least that is how I understand it... |
But this won't work if two threads call getSession()` concurrently; each will get a different session and the last one wins. I haven't looked at the locking code lately, but why not just replace it with a simple |
Hm. Yes, I see what you mean: more concurrency more We might just then have a a regular I'll think more. Thank you! |
How about this? |
} | ||
} | ||
if (this.isSharedSession) { | ||
this.sharedJschSession = jschSession; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why write it back to the field on every call?
In any case, it must be done while the lock is held; otherwise we're back to the possibility of a concurrent update.
Related to https://build.spring.io/browse/INT-MASTERSPRING40-677/
Doesn't look like
DefaultSftpSessionFactory.getSession()
needslocking around
sharedJschSession
getSession()
to store asharedJschSession
into the local variable and if it is
null
or not connected, create anew
JSchSessionWrapper
, connect it and store to thesharedJschSession
back into the
sharedJschSession
property ifthis.isSharedSession
.This way we always deal with
sharedJschSession
anyway if it is validor create a new fresh one if that is invalid.
Without locking we always get an actual state of the
sharedJschSession
and don't fall into the race condition when
sharedJschSession
is invalid,but we can't connect to the SFTP channel from the
sftpSession.connect()