Skip to content

Upload from multiple threads throws 'The session is not open' error #1493

Description

@anandgmenon

This is very similar to #369

I am using 2024.1.0 version of the library and connecting to an Azure Storage SFTP server. I am trying to upload 50 files in parallel using the same connection and it throws this exception

System.InvalidOperationException: The session is not open.
at Renci.SshNet.SubsystemSession.EnsureSessionIsOpen()
at Renci.SshNet.Sftp.SftpSession.SendRequest(SftpRequest request)
at Renci.SshNet.Sftp.SftpSession.RequestWrite(Byte[] handle, UInt64 serverOffset, Byte[] data, Int32 offset, Int32 length, AutoResetEvent wait, Action`1 writeCompleted)
at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
at FunctionApp1.Function1.<>c__DisplayClass2_2.<UploadFiles>b__1() in E:\src\SFtp-Function\FunctionApp1\Function1.cs:line 62
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at FunctionApp1.Function1.<>c__DisplayClass2_1.<<UploadFiles>b__0>d.MoveNext() in E:\src\SFtp-Function\FunctionApp1\Function1.cs:line 62
--- End of stack trace from previous location ---
at FunctionApp1.Function1.UploadFiles() in E:\src\SFtp-Function\FunctionApp1\Function1.cs:line 68

My code looks like this:

staticasyncTaskUploadFiles(){varbytes=File.ReadAllBytes("E:\\src\\ConsoleApp\\ConsoleApp1\\6 MB");intdegreeOfParallelism=50;varoptions=newParallelOptions();options.MaxDegreeOfParallelism=degreeOfParallelism;try{using(varclient=newSftpClient(_connectionInfo)){client.KeepAliveInterval=TimeSpan.FromMinutes(30);client.OperationTimeout=TimeSpan.FromMinutes(2);client.ConnectionInfo.MaxSessions=100;client.Connect();vartasks=Enumerable.Range(1,50).AsParallel().WithDegreeOfParallelism(degreeOfParallelism).Select(async i =>{Console.WriteLine($"Uploading file {i}");awaitTask.Run(()=>client.UploadFile(newMemoryStream(bytes),$"upload/6MB_{i}",true));Console.WriteLine($"Upload file {i} completed");return;});awaitTask.WhenAll(tasks);}}catch(Exceptionex){Console.WriteLine(ex.Message);}}

I tried the smilar scenario with the WINSCP library as well to rule out any issues with server and seems to be working fine there

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions