This project is read-only.

CLR gives error while disposing object of Renci.SshNet and error is not caught anywhere.

May 18, 2012 at 8:17 AM

I am using "Renci.SshNet" component in window service. The CLR tries to recover the memory allocated to object of this component and some time it gives error but this error is not caught in try catch block anywhere, so window service is stopped abnormally.

I found following information in event viewer when service was stopped.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
Stack:
   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.Finalize()

First i want to know " why this error is coming"

and for workaround

I want to know "How I can catch this error and stop the abnormal shutdown of window service"?

Coordinator
Jun 5, 2012 at 8:56 PM

Hi,

 

It looks like it disposing something twice.

 

Can you post a code that you using so I could recreate the problem?

 

Thanks,

Oleg

Jun 28, 2012 at 1:14 PM

 

        private bool ConnecttoSFTP()
        {
            bool sftpConnectionEstablished;
            try
            {
                if (this.sftpClient != null && this.sftpClient.IsConnected)
                {
                    sftpConnectionEstablished = true;
                }
                else
                {
                    this.sftpClient = new Renci.SshNet.SftpClient(this.hostAddress, this.sftpPort, this.userId, this.password);
                    this.sftpClient.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 50, 0);
                    this.sftpClient.ConnectionInfo.RetryAttempts = 5;
                    this.sftpClient.BufferSize = (1024 * 30) - 38;
                    this.sftpClient.Connect();
                    sftpConnectionEstablished = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex, LoggerType.PollingService);
                sftpConnectionEstablished = false;
            }

        public void DisconnectSftp()
        {
            try
            {
                if (this.sftpClient != null && this.sftpClient.IsConnected)
                {
                    this.sftpClient.Disconnect();
                    this.sftpClient.Dispose();
                }

                if (this.sftpClient != null)
                    this.sftpClient = null;
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message);
            }
        }