This project is read-only.

SftpFileStream.Dispose throws an exception if the connection is lost.

Nov 26, 2012 at 10:36 AM

I am using the SftpFileStream so that I can support resuming partial downloads. I have the following code:

using (FileStream fs = File.OpenWrite(local))
{
  using (SftpFileStream remote = sftpClient.OpenRead(remotePath))
  {
    remote.CopyTo(fs, (int)sftpClient.BufferSize);
  }
}

This has a problem when the connection is lost during the transfer. The application completely freezes. I traced this down to the dispose method of SftpFileStream where it tries to send a close message to the server:

internal void RequestClose(byte[] handle)
{
    using (var wait = new AutoResetEvent(false))
    {
        var request = new SftpCloseRequest(this.NextRequestId, handle,
            (response) =>
            {
                if (response.StatusCode == StatusCodes.Ok)
                {
                    wait.Set();
                }
                else
                {
                    this.ThrowSftpException(response);
                }
            });

        this.SendRequest(request);

        this.WaitHandle(wait, this._operationTimeout);
    }
}

 The default _operationTimeout is -1 and so this is the cause for the application freeze. I tried changing this timeout, which stopped the freezing, but I'm not sure whether this will have other ramifications, and it also lead to exceptions being thrown in the Dispose method which is a "bad thing". 

Any ideas what I'm doing wrong? or what I can do to get my scenario working?>

Coordinator
Dec 20, 2012 at 7:34 PM

Hi,

 

I think you doing everything correct.

I just looked at a code and I think I found the problem.

 

Can you please load latest source code version and see if you still getting this problem?

 

Thanks,

Oleg