bentonstark / starksoft-aspen

.net / mono security and cryptography library that provides client support for ftps, gnupg, smartcard, and socks / http proxies
108 stars 49 forks source link

download abort problem #25

Closed davidsdt closed 7 years ago

davidsdt commented 7 years ago

Hi,

I am experiencing a problem when I try to Abort a ftp download.

here is how i abort the download: ftp.Abort(); ftp.TransferProgress -= new EventHandler(ftp_TransferProgress); ftp.TransferComplete -= new EventHandler(ftp_TransferComplete);

However, the statement ftp.GetFile(m_remoteFile, localFileStream, false); throws an InnerException = {"Cannot access a disposed object.\r\nObject name: 'System.Net.Sockets.NetworkStream'."} Which is in fact a Starksoft.Aspen.Ftps.FtpsException: at Starksoft.Aspen.Ftps.FtpsClient.GetFile(String remotePath, Stream outStream, Boolean resume)

Could you please check or advise me how to Stop a download properly?

Regards,

davidsdt commented 7 years ago

In addition, it seems it's not possible to close the connection ftp.Close(); after an abort ftp.Abort();

It throws an exception.

bentonstark commented 7 years ago

Abort() simple sends the "ABOR" text command to the server on the control channel. When that happens if the server accepts it is up to the FTP server implementer. You are seeing an exception getting thrown concerning a disposed NetworkStream. I am going to venture a guess you get this exception right after you send the ABOR command to the FTP server? If so, what is happening is the FTP server is accepting the ABOR command and shutting down the TCP sockets when it resets the .NET client side sockets and disposes of the NetworkStream instance. So if in fact this is what you are seeing then you are actually using the Abort() command correctly. The FtpClient doesn't know why the connection died and simply reports the exception that it can not longer transfer data. I would agree that is a bit confusing but you can simply catch that exception and ignore it if you know you aborted the transfer.

For the second question I need the exception that is being thrown by ftp.Close() after ftp.Abort(). The Close() method should not be throwing an exception.

davidsdt commented 7 years ago

Simply catch the exception and ignore it is what i do.

The error i get with Close() is when i debug via the development environment.

Managed Debugging Assistant 'AsynchronousThreadAbort' has detected a problem in 'xxx.vshost.exe'.

Additional information: User code running on thread 14868 has attempted to abort thread 18000. This may result in a corrupt state or resource leaks if the thread being aborted was in the middle of an operation that modifies global state or uses native resources. Aborting threads other than the currently running thread is strongly discouraged.

davidsdt commented 7 years ago

you can see it if you set your Exception settings to catch "AsynchronousThreadAbort" from Managed Debugging Assistants group.

bentonstark commented 7 years ago

AsynchronousThreadAbort is an internal exception. You can ignore it. Your debugger just happens to be configured to catch handled exceptions. You can reconfigure the debugger in Visual Studio from catching this type of handled exceptions. Basically, there is a monitor queue thread that is looking for events (messages) coming from the FTP server and when you call Close() that shuts down the monitor queue thread.