5

Closed

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

description

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
Client.Connect();
Console.WriteLine(Client.IsConnected); //Prints True
Client.Disconnect();
Console.WriteLine(Client.IsConnected); //Prints True
}
Closed Jun 6, 2012 at 1:31 PM by olegkap
Fixed in 16911.Please let me know if you still experience this problem.Thanks.

comments

Kenneth_aa wrote Apr 27, 2012 at 5: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.

Kenneth_aa wrote Apr 29, 2012 at 7: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 8:54 AM

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

http://sshnet.codeplex.com/discussions/337277

EdSF wrote May 20, 2012 at 4:47 AM

Perhaps related to a closed (IMHO should be re-opened) issue:
http://sshnet.codeplex.com/workitem/792

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 7:58 PM

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

Thanks,
Oleg

juandi wrote Nov 21, 2012 at 10:07 AM

Hi everybody

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

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

                       // Do staff with SftpClient

                        client.Disconnect();
                        do
                        {
                            Thread.Sleep(1000);
                        } while (client.IsConnected);
}

It will stay looping forever.