sparkeh9/CoreFTP

LogoutAsync throws error

Opened this issue · 0 comments

Hi,

When i try to logout, I am getting many (but not always) an error on the LogoutAsync() method.

This is my callstack:

Error occured in TryUploadFileAsync 3/10: System.IO.IOException: Unable to read data from the transport connection: Connection timed out. ---> System.Net.Sockets.SocketException: Connection timed out
2017-10-02T12:13:39.674972357Z          at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
2017-10-02T12:13:39.674975357Z          --- End of inner exception stack trace ---
2017-10-02T12:13:39.674978057Z          at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
2017-10-02T12:13:39.674980957Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.ReadLine(Encoding encoding, CancellationToken token)
2017-10-02T12:13:39.674983557Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.<ReadLines>d__52.MoveNext()
2017-10-02T12:13:39.674986357Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.<GetResponseAsync>d__57.MoveNext()
2017-10-02T12:13:39.674989157Z       --- End of stack trace from previous location where exception was thrown ---
2017-10-02T12:13:39.674991757Z          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2017-10-02T12:13:39.674994357Z          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2017-10-02T12:13:39.674996857Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.<SendCommandAsync>d__56.MoveNext()
2017-10-02T12:13:39.675004258Z       --- End of stack trace from previous location where exception was thrown ---
2017-10-02T12:13:39.675007558Z          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2017-10-02T12:13:39.675010058Z          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2017-10-02T12:13:39.675012758Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.<SendCommandAsync>d__55.MoveNext()
2017-10-02T12:13:39.675015358Z       --- End of stack trace from previous location where exception was thrown ---
2017-10-02T12:13:39.675017958Z          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2017-10-02T12:13:39.675020758Z          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2017-10-02T12:13:39.675023458Z          at CoreFtp.Infrastructure.Stream.FtpControlStream.<SendCommandAsync>d__54.MoveNext()
2017-10-02T12:13:39.675033658Z       --- End of stack trace from previous location where exception was thrown ---
2017-10-02T12:13:39.675036558Z          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2017-10-02T12:13:39.675039958Z          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2017-10-02T12:13:39.675042558Z          at CoreFtp.FtpClient.<LogOutAsync>d__35.MoveNext()
2017-10-02T12:13:39.675044958Z       --- End of stack trace from previous location where exception was thrown ---
2017-10-02T12:13:39.675047558Z          at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2017-10-02T12:13:39.675050258Z          at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2017-10-02T12:13:39.675052858Z          at Ingester.Application.Services.TargetFtp.<TryUploadFileAsync>d__9.MoveNext() 

are there any issues known about this?

Do I even need to use a LogOut when using a using?

below is my code for clearity:

public async Task<Result> UploadAsync(MemoryStream stream, string fileName)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(nameof(fileName));
            }

            using (var client = _ftpClientFactory.Create())
            {
                var result = await TryUploadFileAsync(client, stream, fileName, 0);
                return result;
            }
        }

        private async Task<Result> TryUploadFileAsync(IFtpClient client, Stream stream, string fileName, int currentTry)
        {
            try
            {
                _logger.LogDebug("Logging in..");
                await client.LoginAsync();
                _logger.LogDebug("Logged in.");

                _logger.LogDebug($"Working directory: {client.WorkingDirectory}");
                if (!string.IsNullOrWhiteSpace(_config.WorkingDirectory) && 
                    !string.Equals(client.WorkingDirectory, "/" + _config.WorkingDirectory, StringComparison.InvariantCultureIgnoreCase))
                {
                    await client.ChangeWorkingDirectoryAsync(_config.WorkingDirectory);
                    _logger.LogDebug($"Changed working directory to {client.WorkingDirectory}");
                }

                using (var writeStream = await client.OpenFileWriteStreamAsync(fileName))
                {
                    stream.Position = 0;

                    _logger.LogDebug($"Copy file '{fileName}'");
                    await stream.CopyToAsync(writeStream);
                    _logger.LogDebug($"File '{fileName}' copied.");

                    _logger.LogDebug("Logging out");
                    await client.LogOutAsync();
                    _logger.LogDebug("Logged out.");

                    return Result.Ok();
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Error occured in TryUploadFileAsync {currentTry}/{NumberOfTriesUpload}: {e}");
                if (currentTry < NumberOfTriesUpload)
                {
                    currentTry++;
                    //many connections/memory, wait a few seconds
                    await Task.Delay(WaitSeconds * 1000);
                    return await TryUploadFileAsync(client, stream, fileName, currentTry);
                }

                return Result.Fail("Number of tries to upload exceeded.");
            }
        }