Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException: The session is not open. #369

Closed
kasharmaIVP opened this issue Jan 11, 2018 · 10 comments · Fixed by #1362
Closed

System.InvalidOperationException: The session is not open. #369

kasharmaIVP opened this issue Jan 11, 2018 · 10 comments · Fixed by #1362

Comments

@kasharmaIVP
Copy link

Hi
I am running on renci latest version 2016.1.0.0

Trying to upload/poll/download and delete in sequence using 4 different static SFTP connections for these individual operations. This all happens in multi-threaded environment with a throttle of 100 parallel threads.

The dispose happens at once, only when all the threads are finished.

Problem : After a few files I start getting the following error in either of these operations (upload/poll/download/delete).


_SFTP - Error in SFTP Upload. Retry attempt number : 9. Exception : System.InvalidOperationException: The session is not open.
   at Renci.SshNet.SubsystemSession.EnsureSessionIsOpen()
   at Renci.SshNet.SubsystemSession.SendData(Byte[] data)
   at Renci.SshNet.Sftp.SftpSession.SendRequest(SftpRequest request)
   at Renci.SshNet.Sftp.SftpSession.RequestRealPath(String path, Boolean nullOnError)
   at Renci.SshNet.Sftp.SftpSession.GetCanonicalPath(String path)
   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, Action`1 uploadCallback)_

Following is the GetInstance method to create the 4 static connection on first request:

public SftpClient GetInstance(string instanceID)
        {


            if (_edmSftpClient == null || _edmSftpClient.ContainsKey(instanceID) == false)
            {
                lock (((ICollection)_edmSftpClient).SyncRoot)
                {
                    if (_edmSftpClient == null || _edmSftpClient.ContainsKey(instanceID) == false)
                    {
                        string _domain = _credential.Domain.Replace("sftp://", "").Replace("ftp://", "");
         
                        ConnectionInfo ConnNfo = new ConnectionInfo(_domain, 22, _credential.UserName, new AuthenticationMethod[]
                            {
                                new PasswordAuthenticationMethod(_credential.UserName, _credential.Password),                             
                                new PrivateKeyAuthenticationMethod(_credential.UserName,_pkFileClient.ToArray()),
                                    }
                                );

                        logger.Info(string.Format("Creating SFTP for blockInstance: {0}", instanceID));
                        if (_credential.Domain != null)
                            _edmSftpClient.Add(instanceID, new SftpClient(ConnNfo) { OperationTimeout = TimeSpan.FromDays(1), KeepAliveInterval = TimeSpan.FromDays(1) });
                        else
                            throw new Exception("SFTP Domain not set.");
                    }
                }
            }
            if (_edmSftpClient[instanceID].IsConnected == false)
            {
                lock (((ICollection)_edmSftpClient).SyncRoot)
                {
                    if (_edmSftpClient[instanceID].IsConnected == false)
                    {
                        _edmSftpClient[instanceID].ConnectionInfo.MaxSessions = 99999999;
                        _edmSftpClient[instanceID].ConnectionInfo.Timeout = TimeSpan.FromDays(1);
                        _edmSftpClient[instanceID].Connect();
                    }
                }
            }
            _edmSftpClient[instanceID].ErrorOccurred += (object sender, ExceptionEventArgs e) =>
            {
                logger.Error(string.Format("_edmSftpClient.ErrorOccurred event captured with exception {0}", e != null && e.Exception != null ? e.Exception.ToString() : "no error message recorded"));
            };
            return _edmSftpClient[instanceID];
        }

And here is the sample Upload method that I have written. Trying for 10 retries:

public void Upload(string remotePath, string remoteFileName, byte[] stream)
       {
           EDMSftpClient _sftpClient = null;
           int _retryCount = 10;
           while (true)
           {
               try
               {
                   logger.Info(string.Format("Uploading File : {0}. RemotePath : {1}", remoteFileName, remotePath));

                   _sftpClient = new EDMSftpClient(Credential);
                   _sftpClient._pkFileClient = pkFile;
                   string r = GetRemotePathNoServer(remotePath, remoteFileName);
                   var _uInstance = _sftpClient.GetInstance("U" + _blockInstanceID);
                   _uInstance.UploadFile(new MemoryStream(stream), r, (x) =>
                   {
                       logger.Info(string.Format("Uploaded File : {0}. RemotePath : {1}", remoteFileName, remotePath));
                   });
                   break;
               }
               catch (Exception ex)
               {
                   _retryCount--;
                   logger.Error(string.Format("Error in SFTP Upload. Retry attempt number : {0}. Exception : {1}", _retryCount, ex));
                   if (_retryCount == 0)
                       throw;
                   Thread.Sleep(1000 * _retryCount);

               }
           }
       }

This happens intermittently after some time. Have tried playing around with properties like ConnectionInfo.MaxSessions, KeepAliveInterval, ConnectionInfo.Timeout. But no luck.
And the confusion is the the _edmSftpClient[instanceID].IsConnected property evaluates to true while I get this error.

Can you please suggest where am I going wrong. Could it be SFTP server setting issue or a code bug.

@drieseng
Copy link
Member

The server probably closed the channel.
Have you tried looking at the server log?

If you have a client (eg. SftpClient) that has successfully established an SSH connection to a given server, the server may very well reject subsequent requests (eg. a request to open a channel, or a sftp subsystem request).

@VAllens
Copy link

VAllens commented May 10, 2018

Unfortunately, I also get such annoying mistakes.
Is there a solution to this issue?

@avr2222
Copy link

avr2222 commented Aug 31, 2021

I am also getting the same error. Any solutions?

@DoctorChanqiu
Copy link

Does anyone know how to solve this?

@DoctorChanqiu
Copy link

I also encountered the same problem

@WojciechNagorski
Copy link
Collaborator

Do you still use 2016.1.0.0?

@DoctorChanqiu
Copy link

@WojciechNagorski Hello, my SSH.NET version is 2020.0.1

@DoctorChanqiu
Copy link

@WojciechNagorski
image

@andythomasdev
Copy link

andythomasdev commented Feb 12, 2024

Any updates on this issue @WojciechNagorski? I'm experiencing this in 2020.0.2 when using an instance of an SftpClient inside a Parallel.ForEach() loop to upload files.

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.RequestRealPath(String path, Boolean nullOnError) at Renci.SshNet.Sftp.SftpSession.GetCanonicalPath(String path) at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action'1 uploadCallback)

@lucasdegang
Copy link

Feedback on version "2024.1.0":
Everything is working fine! Just a friendly reminder: if the issue persists, consider using a Polly retry. Also, verify if the client is connected using client.IsConnected() method. If not connected, initiate a connection as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants