Bug in SftpClient dispose

Mar 5, 2013 at 1:06 PM
Edited Mar 5, 2013 at 1:07 PM
Hi Oleg,

I found a bug in how you are handling disposing of SftpClient (and possibly other clients). In short I use SftpClient in a using{} block, during Dispose it is possible that an Exception can occur closing the Channel (e.g. SocketException), which lead to not setting GC.SuppressFinalize and eventually the objects will be GC'd by the framework which leads to another exception being thrown, one which I am obviously unable to catch.

Here is the stack trace of the exception being thrown during standard Dispose from using{} block (note this exception I'm catching at the moment):
   System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
   at Renci.SshNet.Session.SocketWrite(Byte[] data) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Session.NET.cs:line 178
   at Renci.SshNet.Session.SendMessage(Message message) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Session.cs:line 742
   at Renci.SshNet.Channels.Channel.Close(Boolean wait) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Channels\Channel.cs:line 523
   at Renci.SshNet.Channels.ChannelSession.Close(Boolean wait) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Channels\ChannelSession.cs:line 108
   at Renci.SshNet.Channels.Channel.Dispose(Boolean disposing) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Channels\Channel.cs:line 706
   at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean disposing) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\SubsystemSession.cs:line 224
   at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean disposing) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\Sftp\SftpSession.cs:line 234
   at Renci.SshNet.SftpClient.Dispose(Boolean disposing) in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\SftpClient.cs:line 1578
   at Renci.SshNet.BaseClient.Dispose() in d:\Temp\sshnet-22965\Renci.SshClient\Renci.SshNet\BaseClient.cs:line 210
And here is the exception that follows when .NET does it's garbage collection on this same SftpClient (this exception I cannot catch):
Exception Info: Renci.SshNet.Common.SshConnectionException
   at Renci.SshNet.Session.SendMessage(Renci.SshNet.Messages.Message)
   at Renci.SshNet.Channels.Channel.Close(Boolean)
   at Renci.SshNet.Channels.ChannelSession.Close(Boolean)
   at Renci.SshNet.Channels.Channel.Dispose(Boolean)
   at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SubsystemSession.Finalize()
Mar 5, 2013 at 1:11 PM
Edited Mar 5, 2013 at 1:12 PM
I see this is the same bug as per Issue 1514
Mar 7, 2013 at 3:42 PM

Yes, this is the same bug I think.
Can you please check 23469 commit and see if you still having this problem?

Sep 26, 2013 at 5:59 PM
Is this fixed in the code?

I'm still getting the exception when compiling from source code.

However, the published binary didn't seem to throw the exception.
Oct 24, 2013 at 9:45 PM
I believe I continue to have this issue in release 2013.4.7 (associated with commit 25006).

Exception Info: System.Net.Sockets.SocketException
at Renci.SshNet.Session.SocketWrite(Byte[])
at Renci.SshNet.Session.SendMessage(Renci.SshNet.Messages.Message)
at Renci.SshNet.Channels.Channel.SendMessage(Renci.SshNet.Messages.Connection.ChannelCloseMessage)
at Renci.SshNet.Channels.Channel.Close(Boolean)
at Renci.SshNet.Channels.ChannelSession.Close(Boolean)
at Renci.SshNet.Channels.Channel.Dispose(Boolean)
at Renci.SshNet.Channels.ChannelSession.Dispose(Boolean)
at Renci.SshNet.Channels.Channel.Dispose()
at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean)
at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
at Renci.SshNet.Sftp.SubsystemSession.Dispose()
at Renci.SshNet.SftpClient.Dispose(Boolean)
at Renci.SshNet.BaseClient.Finalize()
Nov 18, 2013 at 2:18 PM
I have also observed this issue using version 2013.4.7

When is a new release planned?

I was not able to compile from source directly from the svn repo using MSVS express 2012.
Nov 18, 2013 at 2:29 PM
Ok, I wasn't able to build the entire solution but could build the Renci.sshnet.dll

I'm testing it now...
Apr 6 at 4:31 PM
A new beta release is now available on codeplex and nuget.org.
Let me know if you can still reproduce this issue.
Marked as answer by drieseng on 4/6/2014 at 9:31 AM