0

场景:ai 有一个与 SFTP 服务器建立连接并下载文件的应用程序。执行此操作的代码:

while (true)
{
    try
    {
        var connectionInfo = // creating a new connection info of a Renci.SshNet lib
        var client = new SftpClient(connectionInfo);
        client.Connect();

        using (SftpFileStream sftpFileStream = client.OpenRead(path)
        {
            using (FileStream fileStream = new FileStream(filePath, FileMode.CreateNew))
            {
                int BUFFER_SIZE = 2048;
                byte[] buffer = new byte[BUFFER_SIZE];

                int readBytes = sftpFileStream.Read(buffer, 0, BUFFER_SIZE);

                while (readBytes > 0)
                {
                    fileStream.Write(buffer, 0, readBytes);
                    readBytes = sftpFileStream.Read(buffer, 0, BUFFER_SIZE);
                }
            }
        }

        client.Dispose();
    }
    catch (Exception e)
    {
        log.Error(e);
    }

    Thread.Sleep(1 hour);
}

在该行client.Connect()中引发了第一个错误:

Renci.SshNet.Common.SftpPathNotFoundException: No such file at Renci.SshNet.Sftp.SubsystemSession.WaitHandle(WaitHandle waitHandle, TimeSpan operationTimeout) at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError) at Renci.SshNet.Sftp.SftpFileStream..ctor(SftpSession session, String path, FileMode mode, FileAccess access, Int32 bufferSize, Boolean useAsync) at Renci.SshNet.Sftp.SftpFileStream..ctor(SftpSession session, String path, FileMode mode, FileAccess access) at Renci.SshNet.SftpClient.OpenRead(String path) at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.BaixarArquivoPABXSFTP(ConfiguracaoColetor configuracaoColetor, String arquivoPABX) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 446 at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.ProcessarArquivoSFTP(SftpFile arquivo, ConfiguracaoColetor configuracaoColetor, List'1 listaBilhetes) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 658 at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.ColetarBilhetes(ConfiguracaoColetor configuracaoColetor, List'1 listaBilhetes, String ultimoArquivoTransmitido) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 73 at Infraero.TINE3.STTColetor.Negocio.Coletor.ColetarBilhetes() in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Coletor.cs:line 56 at Infraero.TINE3.STTColetor.WindowsService.ServicoColetor..ctor() in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.WindowsService\ServicoColetor.cs:line 85

1 小时后,该过程尝试再次下载文件,但在行中引发了一个新错误client.Connect()

System.Threading.Tasks.TaskSchedulerException: An exception was thrown by a TaskScheduler. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark) at System.Threading.Thread.Start(StackCrawlMark& stackMark) at System.Threading.Thread.Start(Object parameter) at System.Threading.Tasks.ThreadPoolTaskScheduler.QueueTask(Task task) at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection) at System.Threading.Tasks.Task.InternalStartNew(Task creatingTask, Object action, Object state, CancellationToken cancellationToken, TaskScheduler scheduler, TaskCreationOptions options, InternalTaskOptions internalOptions, StackCrawlMark& stackMark) at System.Threading.Tasks.TaskFactory.StartNew(Action action, TaskCreationOptions creationOptions) at Renci.SshNet.Session.ExecuteThread(Action action) at Renci.SshNet.Session.Connect() at Renci.SshNet.BaseClient.Connect() at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.ConectarServidorSFTP(String servidor, Int32 porta, String login, String senha) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 551 at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.RequisitarArquivoServidorSFTP(ConfiguracaoColetor configuracaoColetor) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 519 at Infraero.TINE3.STTColetor.Negocio.Drivers.DriverAbstratoArquivoFTP.ColetarBilhetes(ConfiguracaoColetor configuracaoColetor, List'1 listaBilhetes, String ultimoArquivoTransmitido) in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Drivers\DriverAbstratoArquivoFTP.cs:line 67 at Infraero.TINE3.STTColetor.Negocio.Coletor.ColetarBilhetes() in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.Negocio\Coletor.cs:line 56 at Infraero.TINE3.STTColetor.WindowsService.ServicoColetor..ctor() in D:\SVN\STT\trunk\3-0_CodigoFonte_Coletor\3-4_SRC\Infraero.TINE3.STTColetor.WindowsService\ServicoColetor.cs:line 85

1天后,第二个错误继续,app的内存使用量从60MB增加到800MB。

任何人都知道问题可能是什么?

4

1 回答 1

2

快速浏览您正在使用的库的代码库 ( http://sshnet.codeplex.com/SourceControl/changeset/view/18974 ) 表明它SftpClient也实现了IDisposable,但您没有清理或关闭它当某事失败时。您应该尝试以下方法:

try
{
    var connectionInfo = // something
    using(var client = new SftpClient(connectionInfo))
    {
        client.Connect();
        using(var sftpFileStream = client.OpenRead(path))
        {
            // download the file
        }
    }
}

它也可能是您正在使用的库内部的内存泄漏。

于 2012-08-30T14:09:28.583 回答