In some error cases, an unhandled exception might be thrown from a worker thread (uncatchable)


There is a race condition that can cause an unhandled exception to be thrown. Take a look at the following code:
    // "server" needs to be changed to a real SSH server
    // The credentials do not need to be valid
    using (SftpClient client = new SftpClient("server", "user", "password"))
        client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e)
            e.CanTrust = false;

        client.Connect(); // <-- Exception from untrusted host key bubbles up here
    } // <-- End of using block causes client.Dispose() to be called, causing an unhandled exception in another thread
catch (Exception ex)
    // This code isn't executed because of the unhandled exception from another thread
    Console.WriteLine("There was an error connecting to the server: {0}", ex.Message);
On my machine, this results in the following exception almost every time:
Renci.SshNet.Common.SshConnectionException was unhandled
  Message=Client not connected.
       at Renci.SshNet.Session.SendMessage(Message message) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 658
       at Renci.SshNet.Session.SendDisconnect(DisconnectReason reasonCode, String message) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 893
       at Renci.SshNet.Session.RaiseError(Exception exp) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 1914
       at Renci.SshNet.Session.MessageListener() in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 1594
       at Renci.SshNet.Session.<Connect>b__4() in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 529
       at Renci.SshNet.Session.<>c__DisplayClass3d.<ExecuteThread>b__3c(Object o) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.NET40.cs:line 25
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
The problem is that Session is getting disposed (by the using statement) prior to Session.SendDisconnect being called from another thread while the client attempts to clean up from the exception thrown by the lack of trust in the host key.
Closed Sep 17, 2016 at 8:44 AM by drieseng
If you can still reproduce this issue with version 2016.0.0 (or higher) of SSH.NET, then please submit an issue at our new home.


dcormier wrote Dec 16, 2013 at 3:22 PM

I've submitted a patch (ID 15568) to address this.

wrote Dec 18, 2013 at 4:36 PM

wrote Dec 18, 2013 at 4:37 PM

glenncarr wrote Jul 21, 2015 at 12:50 PM

We're seeing a similar issue, which may be related:
   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.Sftp.SubsystemSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
   at Renci.SshNet.SftpClient.Dispose(Boolean)
   at Renci.SshNet.BaseClient.Finalize() 

dcormier wrote Jul 21, 2015 at 4:32 PM

Yes, I'm pretty sure that is related. If you go here and search for 15568 you can find a link to download my patch. Be sure to vote up this issue.

wrote Mar 17, 2016 at 3:38 AM

abeytr wrote Mar 17, 2016 at 3:44 AM

Even we are facing same issue which causes our main application to crash and stop.

Is there any work around for this?

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
   at Renci.SshNet.Session.RaiseError(System.Exception)
   at Renci.SshNet.Session.MessageListener()
   at Renci.SshNet.Session.<Connect>b__4()
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

wrote Sep 17, 2016 at 8:44 AM