Skip to content

Invalid handle for simultaneous downloads. #1731

Open

Description

@m0a0k0s

I’m using a single SftpClient to download multiple files simultaneously. On one of a servers, I get "Invalid handle" error from time to time.
SSH.NET version 2024.2.0, .Net 9.
In connection logs I only see this version: Remote version: SSH-2.0-2.0

			"HResult": -2146233088,
			"Message": "Invalid handle.",
			"Source": "Renci.SshNet",
			"StackTrace": "   at Renci.SshNet.Sftp.SftpSession.RequestClose(Byte[] handle)\r\n   at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)\r\n   at Renci.SshNet.SftpClient.<>c__DisplayClass56_0.<BeginUploadFile>b__0()\r\n--- End of stack trace from previous location ---\r\n   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)\r\n--- End of stack trace from previous location ---\r\n
			"TargetSite": "Void RequestClose(Byte[])",
			"Type": "Renci.SshNet.Common.SshException"

   at Renci.SshNet.Sftp.SftpSession.RequestClose(Byte[] handle)
   at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
   at Renci.SshNet.SftpClient.<>c__DisplayClass56_0.<BeginUploadFile>b__0()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)

Activity

  1. Rob-Hague commented on Nov 5, 2025

    @Rob-Hague
    Collaborator

    Please give it a try with 2025.1.0? There have been 2 relevant fixes since 2024.2.0: #1623 and #1643

  2. m0a0k0s commented on Nov 5, 2025

    @m0a0k0s
    Author

    ok, I updated to 2025.1, will check next week

  3. m0a0k0s commented on Nov 18, 2025

    @m0a0k0s
    Author

    Finally updated to 2025.1.0 with dsa rollbacked. Error is still reproducing.

    			"HResult": -2146233088,
    			"Message": "Invalid handle.",
    			"Source": "System.Private.CoreLib",
    			"StackTrace": "   at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback, Boolean isAsync, CancellationToken cancellationToken)\r\n
    			"TargetSite": "Void Throw()",
    			"StatusCode": "9",
    			"Type": "Renci.SshNet.Common.SftpException"
    
    
  4. juliannamello commented on Dec 16, 2025

    @juliannamello

    I'm encountering this as well. I tested with file upload

    This code fails - not every single time, but pretty consistently:

            var client = new Renci.SshNet.SftpClient(_configuration["SftpHost"], _configuration["SftpUsername"], _configuration["SftpPassword"]);
            await client.ConnectAsync(ct);
    
            await Parallel.ForEachAsync(Enumerable.Range(1, 16), new ParallelOptions
            {
                MaxDegreeOfParallelism = 5,
                CancellationToken = ct,
            }, async (i, token) =>
            {
                using FileStream fs = File.OpenRead($"C:/tmp/{i}.txt");
                await client.UploadFileAsync(fs, $"jmello_test/{i}.txt", ct);
            });
    

    I tried increasing MaxDegreeOfParallelism to 16 in the above example and never saw an error

    I also tried switching to synchronous execution and did not see an error:

            var ct = CancellationToken.None;
    
            var client = new Renci.SshNet.SftpClient(_configuration["SftpHost"], _configuration["SftpUsername"], _configuration["SftpPassword"]);
            await client.ConnectAsync(ct);
    
            Parallel.ForEach(Enumerable.Range(1, 16), new ParallelOptions
            {
                MaxDegreeOfParallelism = 5,
                CancellationToken = ct,
            }, async (i, token) =>
            {
                using FileStream fs = File.OpenRead($"C:/tmp/{i}.txt");
                client.UploadFile(fs, $"jmello_test/{i}.txt");
            });
    

    Here are the exception details:

      Message: 
        Renci.SshNet.Common.SftpException : Invalid handle.
    
      Stack Trace: 
        SubsystemSession.<WaitOnHandleAsync>g__DoWaitAsync|38_0[T](TaskCompletionSource`1 tcs, Int32 millisecondsTimeout, CancellationToken cancellationToken)
        SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback, Boolean isAsync, CancellationToken cancellationToken)
        <<FailsWhenAll>b__0>d.MoveNext() line 26
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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