This project is read-only.

SocketException crashes app

Sep 23, 2011 at 5:37 PM

Hi, first of all thanks for the Library, I haven't been able to find anything else like it and it was exactly what I was looking for.

Now, to the problem. I've been getting an exception that is causing my app to crash and I'm not sure why, I thought it would be safe as the library is only used from within a try/catch.

My app is built on c# .NET 4.0 and is running on Windows Server 2008 R2 Standard x64. The code looks a bit like this:

SftpClient sftp  = new SftpClient(server, userName, new PrivateKeyFile[] { new PrivateKeyFile(identityFilePath) });
if (!sftp.IsConnected)
using (Stream fs = new FileStream(fullLocalFilePath, FileMode.Create))
                this.sftp.DownloadFile(fullServerFilePath, fs);

but unfortunately I've not been able to get the error whilst debugging, so I'm not sure at which point the Exception is being thrown.

The details of the exception are:

Application: FSL Services.exeFramework Version: v4.0.30319Description: The process was terminated due to an unhandled exception.Exception Info: System.Net.Sockets.SocketExceptionStack:   at Renci.SshNet.Session.WaitHandle(System.Threading.WaitHandle)   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()


I'm not that bothered about the fact that an exception is being thrown, after all you cannot rely on a remote connection. But what worries me is that this exception is not being caught and handled by my application. If anyone could explain this then I'd be very grateful.




Sep 23, 2011 at 5:44 PM

Not sure if that would be a complete answer.

In the library, I am trying to handle only exception that I am expecting to happen.

For example, if  initiated a disconnect that I would expect socket exception to be thrown and if that happens then I dont rethrow it back and user never sees it.


Other then that I passing all errors back to the user. The reason is that I cannot predict what errors might happend and it up to the user to decide how to handle it, whether to try FTP file again or ask user for an input.

I also have an opinion that I rather get an exception that it will be swollen by the library and user never sees it and never knows what happend. I've seen quite few empty catch blocks like that: catch { }.


In your specific case, since it throws a SocketException, it usually has a code for exception, where you can determine what happend.


Hope it helps,



Sep 27, 2011 at 11:37 AM

I still don't understand why I am not able to catch the Exception.

Perhaps I am using your library incorrectly, I see there is a public event handler in the BaseClient class, called ErrorOccurred. Can errors be handled by attaching to this event handler? Do you have any advice on how to handle errors thrown by the library? My Try/Catch is not working.




Sep 28, 2011 at 2:39 PM


completly forgot about this.

You right, there are some situations where error is thrown on different thread and cannot be transmitted to the user so not to swallow it and report it back to the user you need to handle "ErrorOccurred" event.


Let me know if it helps.