SftpClient.IsConnected property does not get set to False after call to Disconnect()


This was tested using the latest build for .NET 3.5. I have not tested .NET 4.0 or any clients other than SftpClient.
using (SftpClient Client = new SftpClient(_ConnectionInfo))
Console.WriteLine(Client.IsConnected); //Prints False
Console.WriteLine(Client.IsConnected); //Prints True
Console.WriteLine(Client.IsConnected); //Prints True
Closed Jun 6, 2012 at 2:31 PM by olegkap
Fixed in 16911.Please let me know if you still experience this problem.Thanks.


Kenneth_aa wrote Apr 27, 2012 at 6:14 PM

Looks like (BaseClient.cs) this.Session isn't nulled when disconnecting. Also Session.cs for IsConnected, ok this got a bit complicated..

When BaseClient.Disconnect is called... it checks if connected, calls Session.Disconnect() which sets isDisconnecting = true then sends disconnect message to remote.

When DisconnectMessage is received from remote, Socket.Disconnect(true) is called, then messageListenerCompleted (checked in Session.IsConnected if non-null(=connected)) is disposed/nulled. Possible cause here is the messageListenerCompleted.WaitOne() call (Session.cs:918) Then socket is disposed and nulled.

Note for SshClient, when OnDisconnect is called from Session, forwarded ports are removed. Sftp and Scp implementations does not register for OnDisconnecting.

So at this point (see Session.cs:186):

socket = null (Ignore socket.Connected since its null)
messageListenerCompleted = null
isAuthenticated is... probably true since it's not set to false anywhere. <--- bug?

I only did a code review for this analysis, so if someone can confirm my findings I'd be glad.

wrote Apr 27, 2012 at 6:15 PM

Kenneth_aa wrote Apr 29, 2012 at 8:15 AM

Update: I have added a test for this specific scenario in revision 16227. However Oleg need to analyze how this should be fixed.

StrAbZ wrote May 11, 2012 at 9:54 AM

Some times ago I've add an subject (in discussion), about SftpClient not being disconnected, this is may be linked ?


wrote May 20, 2012 at 5:33 AM

EdSF wrote May 20, 2012 at 5:47 AM

Perhaps related to a closed (IMHO should be re-opened) issue:

I can report exact issue (792) still exists. Call to Disconnect() hangs...and will stop (further) execution.

Just fyi to users - I'd avoid using Tasks / Wait until you've reliably tested for this issue (your Task may never be completed).

olegkap wrote Jun 5, 2012 at 8:58 PM

I thought I solved this problem already but it seems I need to give this another look.


wrote Jun 5, 2012 at 10:46 PM

wrote Jun 6, 2012 at 2:31 PM

juandi wrote Nov 21, 2012 at 11:07 AM

Hi everybody

I have just been able to reproduce this issue again, using .NET 4.0, the SftpClient with version of Renci.SshNet.

using (var client = new SftpClient(Settings.Default.SftpHost, Settings.Default.Username, privateKey))

                       // Do staff with SftpClient

                        } while (client.IsConnected);

It will stay looping forever.

wrote Feb 22, 2013 at 2:18 AM

wrote May 16, 2013 at 1:14 PM