-
-
Notifications
You must be signed in to change notification settings - Fork 931
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
Discussion: How to reliably detect if SftpClient is still connected #843
Comments
That's interesting. We've been running SFTP for over a year now with pooling connections without ever getting this error (at least not noticing it) but recently since a few days ago without any update on our side we started to see some connections to fail with the "The session is not open" exception, too. We're also using IsConnected to check if the connection is still open. |
This behavior seems to be triggered by specific configuration on the server. I only get this for some partners. I presume these partners have session timeout configured and their server disconnects the SFTP session, but does not terminate the connection. |
I am getting the same error. Any suggestions? |
You could implement the above mentioned workaround. I haven't seen this error since I implemented it. |
You mean implemeting this? |
I have pushed the required changes to https://github.com/IgorMilavec/SSH.NET/tree/Issue843 so you can do a private build. |
Ok fine. For now, i will try this and update you. |
This issue seems to be still relevant, can we open a fix to address this? |
Yes |
The proposed fix is imho not correct or at least not complete. Connect() will throw an exception if the connection is still open. That means the following code would throw an exception if the SFTP session is closed, but the connection is still open.
You would obviously not expect this to fail. Not sure what the correct approach/fix would be here, I just thought this was worth mentioning. |
If the server closes the SFTP session but keeps the TCP connection open, this currently causes IsConnected to return true, but any operation fails with "the session is not open". SftpClient.IsConnected now also check sftpSession.IsOpen. Connect() and ConnectAsync() were reworked to take into account that the Session may already/still be open, but the SFTP session may not. This is needed so a reconnect works. fixes sshnet#843 and sshnet#1153
* SftpClient: handle the SFTP session being closed by the server If the server closes the SFTP session but keeps the TCP connection open, this currently causes IsConnected to return true, but any operation fails with "the session is not open". SftpClient.IsConnected now also check sftpSession.IsOpen. Connect() and ConnectAsync() were reworked to take into account that the Session may already/still be open, but the SFTP session may not. This is needed so a reconnect works. fixes #843 and #1153 * Always re-create session --------- Co-authored-by: Rob Hague <[email protected]> Co-authored-by: Igor Milavec <[email protected]>
Am still seeing this issue in a similar connection pooling scenario. After fix #1362 we are seeing "Client not connected." after successful "IsConnected" checks originating from SSH.NET/src/Renci.SshNet/Session.cs Line 1029 in 8ea108a
Is there any way IsConnected can check the socket as well? |
I get the exact the same problem. |
@kev-andrews @toblast I created a new issue #1474 for this. |
It seems to me that BaseClient already checks the socket (with SelectRead instead of SelectWrite but I'm not sure that matters). It also has |
I think you're right, setting a keep alive is the only way to properly avoid an exception. Closed #1474 . |
For reference: there was a bug here after all which should be fixed with next release (see #1474). |
I need to implement SFTP connection pooling. As part of this I need to have long running
SftpClient
s and a way to check if the client is still connected. Initially I usedSftpClient.IsConnected
for this purpose. However, even ifSftpClient.IsConnected
returned true, I many times got this exception when trying to use theSftpClient
instance:Here is the class diagram of the relevant portion of SSH.NET: https://gitmind.com/app/flowchart/75ace876ac90aec4dbce3b61347c6cf9
Based on this architecture, it seems to me that the described problem is caused by
SftpClient
not taking into account theSubsystemSession
's state. As an intermediate workaround I madeBaseClient.IsConnected
virtual and implemented it inSftpClient
as:Initial tests show this solves my problem, but further testing is needed to really claim this.
Can someone with more insight into the codebase confirm my reasoning so far? I would also be thankful if the one could answer the questions that pop up in my mind looking at this class diagram:
The text was updated successfully, but these errors were encountered: