Quantcast
Channel: sshnet Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 1026

Edited Issue: System.NullReferenceException [2013]

$
0
0
Hi All

I have written a program that only downloads a part of the file using SFTP and adds it to the previously downloaded file. Basically resumes download.

Program works perfectly except when I am testing varies conditions for example losing connection ect.

This code is running in a timer.

To reproduce the error:
1. Put a break on "remote.CopyTo(fs, (int)sftp.BufferSize);"
2. Put a break on "catch (System.Net.Sockets.SocketException)"
3. Start timer to run code.
4. As the copy process starts remove network cable (trying to simulate losing connection).
5. Exception will be thrown "System.Net.Sockets.SocketException" after awhile.
6. Plug network cable in.
7. The code will try run again when the timer is triggered and after a random time the exception "System.NullReferenceException" will be thrown(Doesnt take long).

Full Error:
An unhandled exception of type 'System.NullReferenceException' occurred in Renci.SshNet.dll

Additional information: Object reference not set to an instance of an object.

If I break then I see these lines:
Call stack with external code
Renci.SshNet.dll!Renci.SshNet.Sftp.SubsystemSession.SendData(byte[] data)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendMessage(Renci.SshNet.Sftp.SftpMessage sftpMessage)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.SendRequest(Renci.SshNet.Sftp.Requests.SftpRequest request)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpSession.RequestClose(byte[] handle)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Dispose(bool disposing)
Renci.SshNet.dll!Renci.SshNet.Sftp.SftpFileStream.Finalize()
[Native to Managed Transition]

My Code
```
private int Download(string IP, string UserName, string Password, string FileSource, string Num)
{
int fault = 0;

SftpClient sftp = null;

try
{
using (sftp = new SftpClient(IP, UserName, Password))
{
Logging.Log_To_file("SFTP connecting");
sftp.Connect();
Logging.Log_To_file("SFTP ok");

Int64 length;
using (FileStream check = File.OpenWrite(root + num+ @"\" + OldFile))
{
length = check.Length;
}

using (FileStream fs = File.Create(root + num+ @"\" + PartFile))
{
using (SftpFileStream remote = sftp.OpenRead(FileSource))
{
Int64 length2 = remote.Length;
if (length > length2)
{
remote.CopyTo(fs, (int)sftp.BufferSize);
}
else
{
remote.Seek(length, SeekOrigin.Begin);

remote.CopyTo(fs, (int)sftp.BufferSize);
}
}
}

Logging.Log_To_file("SFTP disconnecting...");
sftp.Disconnect();
Logging.Log_To_file("SFTP Done");
sftp.Dispose();
Logging.Log_To_file("SFTP Disposed");
}
}
catch (SshAuthenticationException)
{
Logging.Log_To_file("Permission denied");

fault = 1;
}
catch (SftpPathNotFoundException)
{
Logging.Log_To_file("No such file");

fault = 1;
}
catch (System.Net.Sockets.SocketException)
{
Logging.Log_To_file("Connection timed out");

fault = 1;
}
catch (Exception e)
{
Logging.Log_To_file("Unknown error occurred with SFTP");

fault = 1;
}
return fault;
}
```

Any help with this problem would be much appreciated, thanks.
Comments: ** Comment from web user: drieseng **

In this case the cause is probably this:

The SocketException that was thrown because of the connection loss caused the Dispose() method of SftpFileStream to be invoked.

In the Dispose(bool) method of SftpFileStream we try to close the FXP file handle. This probably failed because the session was closed (due to the loss of connectivity).

Some time later, the GC attempts to finalize the SftpFileStream invoking the Dispose(bool) method again.
In the meantime the session has been disposed, causing the channel to be null.
So when the session attempts to send data through the channel to close the FXP file handle, this causes a NullReferenceException.

I'll modify SftpFileStream to only attempt to close the FXP file handle when the SftpFileStream is being disposed (though invocation of the Dispose() method), and not when being finalized.


Viewing all articles
Browse latest Browse all 1026

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>