This project is read-only.

Disconnect blocks forever

Aug 1, 2011 at 3:51 PM

Greetings, I am running into a problem when I call Disconnect(). No exception gets thrown, but the code execution "hangs" / blocks when I call Disconnect(). Anyone know what might be causing this, or how to fix my problem? Here is my code:



 using (SftpClient sftp = new SftpClient("", 5970, "xxxxx", "xxxx"))
            HttpContext.Current.Response.Write("syncing \n");
            sftp.UploadFile(contentStream, filename);
            HttpContext.Current.Response.Write("sync done \n");
            // calling disconnect kills everything - removing it just so the code will continue
                sftp.Disconnect(); // never gets past here

// never gets here either
                HttpContext.Current.Response.Write("exception when disconnecting \n");

            HttpContext.Current.Response.Write("sync disconnected \n");
            fileUploaded = true;
            HttpContext.Current.Response.Write("sync file uploaded \n");


Aug 1, 2011 at 3:58 PM


Does it happens in console type application or only in web scenario?

Also, what version/code changeset do you use?



Aug 1, 2011 at 4:22 PM

I compiled rev 8760, added [SecurityCritical] to the GetObjectData methods on the exception classes to get this to run in medium trust.

I also just ran my code as a console app, and it works just fine. When I run my web version on my full trust dev server, then it has that same Disconnect problem that I see when I run in medium trust. So it seems to be a problem related to running the code in a ASP.Net context. Any ideas?

Aug 1, 2011 at 4:29 PM


I guess I will need to take a look at it or create a web project where I can test it.

My guess it have something to do with the trust.

I know at the beginning it was working in the web since I was using it internally for the web project I was working on, that how it actually started, but I guess as I was adding more features something caused it to stop.

I dont think I will have time tomorrow, but please log it as an issue and I will take a look at it a little bit later this week.




Aug 1, 2011 at 4:32 PM

Thanks Oleg.

Aug 2, 2011 at 12:59 PM

As a side note, I ended up just not calling Disconnect() in my code, and not creating my sftpclient inside a using(){} block because that was trying to dispose of the client, which also caused the same problem. Since I am only calling this script once a day, not disposing of my client shouldn't cause any problems, even if it is bad practice.

Oct 17, 2011 at 6:49 AM

Any more discoveries on this?  I'm having the same problem.  Running under connecting to a Tumbleweed server, I can connect (sftp) and put/get files, but when I go to the disconnect, it just goes int lala land. 

Note, to connect to Tumbleweed, I needed to limit the encryption to aes-128 - don't know if that matters.  Running the same logic in a windows app works just fine.

Ideas?  Is there a potential threading issue?

I can leave off the disconnect just as rafekemmis did, but this has a really bad smell (IMO).  Is there a way to debug or get more info?

Thanks much,

Dave R

build# 10805

Oct 17, 2011 at 6:23 PM

Hi Dave,


No, I dont have any new discoveries on this since I could not recreate the problem here.

Can you attach a project that causes you a problem or actually send it to me?

You can skip SSH connection info as I can use mine.

Then I could test it again to check for the problem and let you know what I find.

Unfortunatly I am busy now with another urgent project for next Month so it might take a while for me to get to it :(.




Oct 31, 2011 at 9:42 AM


I've got the same problem.

Disconnect() blocks for ever when connecting to a Sysax Server (SSH2 based SFTP).

I can reproduce the problem with the following code :

SftpClient client = new SftpClient("host","user","pass");
client.Disconnect(); // blocks 4 ever

I hope it will be helpful for you to solve the problem. Thank you!


Oct 31, 2011 at 2:16 PM

Hi Seb,


If you executing this code and it hangs, its most likely to do with how server disconects.

I can take a look at it, unfortunatly only at the end of November :( so please bug me again with this issue last week of November sinceI am busy now with other urgent project, but meanwhile if you can do one of the following. Either provide me with test username and password or turn on packet logging, when you use putty for example, and send me this log so I could see what packets beings sent and what he expects.

Some server implemented SSH a little bit different from what SSH specification says, I've seen it before already so I wont be surprised if thats another similar case.


Hope it helps,



Nov 4, 2011 at 2:12 PM

Can you please post what SSH server you are using, and ensure that you use the latest Changeset from the Source tab, there is a Download link on the right side.

Nov 22, 2011 at 3:57 AM



Can you try the latest code and see if the problem still exists?


I just downloaded and isntalled trial version of Sysax Server and it disconnected correctly without hanging.


Please let me know what happens in your case.




Jan 5, 2012 at 1:50 PM

Hi Olegkap,

sorry for latence... I was busy on other projects these days.

So, I downloaded the last release of Renci.SshNet with the SourceFiles and now referenced the project in VS2010 instead of the .dll only.

I still have the same problem when disconnecting from SysaxServer. The program block in Session.Dispose(bool disposing) on the line "this._messageListenerCompleted.WaitOne();"

I tried to put a Timeout (2000ms), and the problem is now different :

SftpClient client = new SftpClient("host","user","pass");
client.Disconnect(); // <== Didn't block anymore :)
client.Disconnect(); // <== Block 4ever (on the second passage) :(
Then, the program blocks in Session.SendMessage(Message message) on the line "var hashData = new byte[4 + packetData.Length];"
Any ideas ?
Thank's for your support,
Jan 6, 2012 at 7:57 AM

Hi Again,

after some tests, I found that the problem appear when message "SSH_MSG_DISCONNECT" is sended.

For now, I put that ugly code on the method Session.SendMessage to fix my problem :

if(message.ToString() == "SSH_MSG_DISCONNECT") return;

In addition, I kept the timeout on Session.Dispose and it seems that everything is working fine now.

I hope this will help you to understand where the problem is. I guess SysaxMultiServer do not respect the SSH standards?

How can I fix my code in a better way ?


Jan 10, 2012 at 3:40 PM


I dont know why it fails second time,

Cause actually have a test case where it tests against that specific scenario.


What server do you use to connect to?

Is it possible for  me to get access to it, some test account, so I could check it out and fix, since I don't get this behavior on any of my servers here?




Jan 11, 2012 at 3:52 PM

Hi Oleg,

thank for your support.

The problem has been fixed by downloading the last version (5.5) of SysaxMultiServer.

Now, Connect(), Disconnect(), and every command I'm using are correct.

I guess the previous version of Sysax was not doing the disconnect process well, and now they fixed that.

Thank you again and have a nice day!


P.S. Sorry for my approximativ english^^

Feb 22, 2012 at 4:22 PM
Edited Feb 23, 2012 at 1:30 PM

I have the same Problem! I wrote a test clr api that "download" about 5000 files from the server. Whenever the connection got timeout disconnect work forever! I used try catch block to catch the clr exception and then disconnect...

when i use the same procedure to copy about 400 files... and disconnect, it worked perfectly without any trouble.


C# CLR for MS SQL 2008, OS Win 7 Pro



Well its a WaitHandler that blocks forever! Couldnt retrieve more information...

Feb 23, 2012 at 1:46 PM


What server versions do you use?

Does it occur with every server or only with specific one?

What WaitHandler did it fail on? I mean class and line of code, since as I remember I tried to put a timeout everywhere so in worth case it will always times out.




Feb 23, 2012 at 2:09 PM
Edited Feb 23, 2012 at 2:39 PM

I'm using OpenSuse 11.1 as Server. Authentication Methode is "KeyboardInteractiveConnectionInfo"


The timeout accured on session.cs on line 650... well after i let kill ssh connection it do not appear anymore. But i have an other problem. 

The program paused itself at last know position of the debugger in session.cs on the line 716. Seems that it starts in ScpClient.cs on line 187.

I'm using changeset 14499 the .net 3.5 Version



I let run wireshark on the VM Server and my machine. Both says that ssh request has been sent but no response has been send back.

Can i Upload a jpg file here somewhere? Anyway, seems that the client wait forever for the response and do not timeout!

Dec 9, 2014 at 2:24 PM
Edited Dec 9, 2014 at 2:30 PM

I have the same problem. I'm using 2014.4.6-beta2

I'm doing a reboot on a linux machine (dropbear ssh server) and disconnecting as fast as i can after that.

The sshclient hangs in the Disconnect method (_messageListenerCompleted.WaitOne()), se comment below.
 private void Disconnect(DisconnectReason reason, string message)
            _isDisconnecting = true;

            // send disconnect message to the server if the connection is still open
            // and the disconnect message has not yet been sent
            // note that this should also cause the listener thread to be stopped as
            // the server should respond by closing the socket
            SendDisconnect(reason, message);

            // disconnect socket, and dispose it

            if (_messageListenerCompleted != null)
                // at this point, we are sure that the listener thread will stop
                // as we've disconnected the socket
                _messageListenerCompleted.WaitOne(); // <-- the code hangs here
                _messageListenerCompleted = null;
Dec 9, 2014 at 3:46 PM
This is a known issue in beta 2 that I already ran into myself. Please submit an issue for this so we don't lose track of it.
You can refer to this discussion.

It happens when the server disconnects, or when there's an unexpected error.
In other words: When It's not the client that initiated the disconnect.

Dec 9, 2014 at 3:57 PM
Thank you for the information.

I have submitted an issue:
Dec 22, 2014 at 2:45 PM
This issue in now fixed in TFS (and in the next beta).
Marked as answer by drieseng on 12/22/2014 at 6:45 AM