Hi,
I have a windows service which synchronizes through SFTP with custom specified intervals, it is installed on 3 machines with 3 different operating systems. On one machine an exception occurs randomly at the disconnect event and I cannot figure out why. On the other machines the problem occurred only once, on this particular machine it occurs every day, sometimes multiple times daily. The application is multithreaded, more than one synchronization jobs can be executed simultaneously. The exception happens only at disconnect, not during upload/download, and always on "empty" sessions where the thread just connects, retrieves a filelist and then disconnects when it notices that there is currently nothing to synchronize.
The exception is a NullReferenceException, message from the log:
```
[2013.05.27. 1:28:45] UNHANDLED EXCEPTION! Message: Az objektumhivatkozás nincs beállítva semmilyen objektumpéldányra. - Stack trace: a következő helyen: Renci.SshNet.Sftp.SubsystemSession.RaiseError(Exception error)
a következő helyen: Renci.SshNet.Sftp.SubsystemSession.Session_ErrorOccured(Object sender, ExceptionEventArgs e)
a következő helyen: System.EventHandler`1.Invoke(Object sender, TEventArgs e)
a következő helyen: Renci.SshNet.Session.RaiseError(Exception exp)
a következő helyen: Renci.SshNet.Session.MessageListener()
a következő helyen: Renci.SshNet.Session.<Connect>b__4()
a következő helyen: Renci.SshNet.Session.<>c__DisplayClass3d.<ExecuteThread>b__3c(Object o)
a következő helyen: System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
a következő helyen: System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
a következő helyen: System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
a következő helyen: System.Threading.ThreadPoolWorkQueue.Dispatch()
a következő helyen: System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
```
The code (most important part):
```
private SftpClient sftpClient = null;
......................................
public void Synchronize( Object stateObject )
{
//................
try
{
sftpClient = new SftpClient( Address, Port, Username, Password ); // don't want to use "using" because of the finally block
try
{
Log( "Connecting to server" );
sftpClient.Connect();
}
catch (Exception ex)
{
Log( "Error while connecting: " + ex.Message );
return; // finally block still will run
}
// ............. filelist, upload/download (this is a large block), everything in try/catch
}
finally
{
if (sftpClient != null)
{
if (sftpClient.IsConnected)
{
Log( "Disconnecting from server" );
sftpClient.Disconnect();
}
sftpClient.Dispose();
}
}
```
I attached the complete log file, please check it out, I don't know why the error is coming and how to avoid it. (Besides the "solution" to enclose the two method calls in the finally block in a try/catch block itself, which isn't too pretty.) Please help if you can.
Thank you, best regards,
Dester
Comments: ** Comment from web user: BobbyCannon **
I have a windows service which synchronizes through SFTP with custom specified intervals, it is installed on 3 machines with 3 different operating systems. On one machine an exception occurs randomly at the disconnect event and I cannot figure out why. On the other machines the problem occurred only once, on this particular machine it occurs every day, sometimes multiple times daily. The application is multithreaded, more than one synchronization jobs can be executed simultaneously. The exception happens only at disconnect, not during upload/download, and always on "empty" sessions where the thread just connects, retrieves a filelist and then disconnects when it notices that there is currently nothing to synchronize.
The exception is a NullReferenceException, message from the log:
```
[2013.05.27. 1:28:45] UNHANDLED EXCEPTION! Message: Az objektumhivatkozás nincs beállítva semmilyen objektumpéldányra. - Stack trace: a következő helyen: Renci.SshNet.Sftp.SubsystemSession.RaiseError(Exception error)
a következő helyen: Renci.SshNet.Sftp.SubsystemSession.Session_ErrorOccured(Object sender, ExceptionEventArgs e)
a következő helyen: System.EventHandler`1.Invoke(Object sender, TEventArgs e)
a következő helyen: Renci.SshNet.Session.RaiseError(Exception exp)
a következő helyen: Renci.SshNet.Session.MessageListener()
a következő helyen: Renci.SshNet.Session.<Connect>b__4()
a következő helyen: Renci.SshNet.Session.<>c__DisplayClass3d.<ExecuteThread>b__3c(Object o)
a következő helyen: System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
a következő helyen: System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
a következő helyen: System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
a következő helyen: System.Threading.ThreadPoolWorkQueue.Dispatch()
a következő helyen: System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
```
The code (most important part):
```
private SftpClient sftpClient = null;
......................................
public void Synchronize( Object stateObject )
{
//................
try
{
sftpClient = new SftpClient( Address, Port, Username, Password ); // don't want to use "using" because of the finally block
try
{
Log( "Connecting to server" );
sftpClient.Connect();
}
catch (Exception ex)
{
Log( "Error while connecting: " + ex.Message );
return; // finally block still will run
}
// ............. filelist, upload/download (this is a large block), everything in try/catch
}
finally
{
if (sftpClient != null)
{
if (sftpClient.IsConnected)
{
Log( "Disconnecting from server" );
sftpClient.Disconnect();
}
sftpClient.Dispose();
}
}
```
I attached the complete log file, please check it out, I don't know why the error is coming and how to avoid it. (Besides the "solution" to enclose the two method calls in the finally block in a try/catch block itself, which isn't too pretty.) Please help if you can.
Thank you, best regards,
Dester
Comments: ** Comment from web user: BobbyCannon **
This is an issue where the channel object is being disposed in parallel with the error events processing. Depending on the machine and how fast one thread is handled than the other will determine if you get an exception. This is why the error seems to be random.