Skip to content

System.ObjectDisposedException: Safe handle has been closed #1524

Closed

Description

@anandgmenon

I am from the Azure Logic Apps team, we use the SSH.NET library in the SFTP connector there.

We have one user which runs into the following error Safe handle has been closed.. the usecase is the following:

The process runs every one minute and it tries to list a directory. We create a connection and try to maintain it. So every time before listing the directory we call sftpClient.IsConnected and if false we create a new connection.

The error happens Safe handle has been closed. after the there's a timeout on that connection is what we have observed.

Renci.SshNet.Common.SshOperationTimeoutException: Operation has timed out.
   at Renci.SshNet.SubsystemSession.WaitOnHandle(WaitHandle waitHandle, Int32 millisecondsTimeout)
   at Renci.SshNet.Sftp.SftpSession.RequestRealPath(String path, Boolean nullOnError)
   at Renci.SshNet.Sftp.SftpSession.GetCanonicalPath(String path)
   at Renci.SshNet.SftpClient.InternalListDirectory(String path, SftpListDirectoryAsyncResult asyncResult, Action`1 listCallback)

After which all the operation using that connection fails with the following exception, even though IsConnected returns true

System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
   at Interop.Kernel32.SetEvent(SafeWaitHandle handle)
   at System.Threading.EventWaitHandle.Set()
   at Renci.SshNet.Sftp.SftpSession.<>c__DisplayClass60_0.<RequestRealPath>b__0(SftpNameResponse response)
   at Renci.SshNet.Sftp.SftpSession.HandleResponse(SftpResponse response)
   at Renci.SshNet.Sftp.SftpSession.TryLoadSftpMessage(Byte[] packetData, Int32 offset, Int32 count)
--- End of stack trace from previous location ---
   at Renci.SshNet.Sftp.SftpSession.RequestRealPath(String path, Boolean nullOnError)
   at Renci.SshNet.Sftp.SftpSession.GetCanonicalPath(String path)
   at Renci.SshNet.SftpClient.InternalListDirectory(String path, SftpListDirectoryAsyncResult asyncResult, Action`1 listCallback)

Is this a bug? Is there any other reliable way to figure out if the connection is alive?

Activity

  1. Rob-Hague commented on Oct 23, 2024

    @Rob-Hague
    Collaborator

    Sounds like #1519

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