FluentFTP: mono: Exception: Safe handle has been closed - fix included (read to end)

NOTE: I have a workaround by using version 32.3.1.0

**FTP OS: Unix type L8

FTP Server: ProFTPD

**Computer OS: RPi Linux Raspbian 5.4.51-v7+ armv7l and Mono 6.12.0.90

**FluentFTP Version: 33.0.2.0

Crash at connect (from my own logfile as no FTP logfile is produced) :

2020-11-16 08:40:26.783 InetSupport: Unknown Exception on FTPS connecting to ftp.meteo-wagenborgen.nl: Safe handle has been closed 2020-11-16 08:40:26.784 InetSupport: Failed FTPS connecting to ftp.meteo-wagenborgen.nl. Files will not be transferred 2020-11-16 08:40:26.784 Exception Unknown : Safe handle has been closed 2020-11-16 08:40:26.786 Data (cont): mscorlib 2020-11-16 08:40:26.798 Data: at System.Runtime.InteropServices.SafeHandle.DangerousAddRef (System.Boolean& success) [0x0001c] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at System.Threading.WaitHandle.WaitOneNative (System.Runtime.InteropServices.SafeHandle waitableSafeHandle, System.UInt32 millisecondsTimeout, System.Boolean hasThreadAffinity, System.Boolean exitContext) [0x00008] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at System.Threading.WaitHandle.InternalWaitOne (System.Runtime.InteropServices.SafeHandle waitableSafeHandle, System.Int64 millisecondsTimeout, System.Boolean hasThreadAffinity, System.Boolean exitContext) [0x00014] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at System.Threading.WaitHandle.WaitOne (System.Int64 timeout, System.Boolean exitContext) [0x00000] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at System.Threading.WaitHandle.WaitOne (System.Int32 millisecondsTimeout, System.Boolean exitContext) [0x00019] in <9f0df102fe6e4cfea29d2e46f585d8a5>:0 at FluentFTP.FtpSocketStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0002e] in <d2e9f2023e584807be8dc039210ee032>:0 at FluentFTP.FtpSocketStream.ReadLine (System.Text.Encoding encoding) [0x00045] in <d2e9f2023e584807be8dc039210ee032>:0 at (wrapper remoting-invoke-with-check) FluentFTP.FtpSocketStream.ReadLine(System.Text.Encoding) at FluentFTP.FtpClient.GetReply () [0x0006e] in <d2e9f2023e584807be8dc039210ee032>:0 at FluentFTP.FtpClient.Execute (System.String command) [0x000da] in <d2e9f2023e584807be8dc039210ee032>:0 at FluentFTP.FtpClient.Authenticate (System.String userName, System.String password) [0x0000c] in <d2e9f2023e584807be8dc039210ee032>:0 at FluentFTP.FtpClient.Authenticate () [0x00017] in <d2e9f2023e584807be8dc039210ee032>:0 at FluentFTP.FtpClient.Connect () [0x00203] in <d2e9f2023e584807be8dc039210ee032>:0 at CumulusUtils.InetSupport…ctor (CumulusUtils.Support s) [0x003d9] in <a0cb7c0feca0466598eb79336532393a>:0 at CumulusUtils.CMXutils.Main (System.String[] args) [0x00103] in <a0cb7c0feca0466598eb79336532393a>:0 2020-11-16 08:40:26.799 Exiting - check log file

No FTP logs were produced as the connect fails

This happens in the connect after the following piece of C# code (the first time FluentFTP call is made):

     {
        try
        {
          clientFluentFTP = new FtpClient(hostname,
                                        username,
                                        password)
          {
            EncryptionMode = FtpEncryptionMode.Explicit,
            DataConnectionEncryption = true,
            SslProtocols = SslProtocols.None,
            DataConnectionType = PassiveFTP ? FtpDataConnectionType.EPSV : FtpDataConnectionType.PORT,
            Encoding = Encoding.UTF8,

            SocketKeepAlive = true,
            ValidateAnyCertificate = true,
            UploadDataType = FtpDataType.Binary
          };

          clientFluentFTP.Connect();
          Sup.LogDebugMessage($" InetSupport: FTPS activated.");
        }
        catch (Exception e) when (e is FtpAuthenticationException || e is FtpCommandException || e is FtpSecurityNotAvailableException)
        {
          Sup.LogDebugMessage($" InetSupport: Exception on FTPS connecting to {hostname}: {e.Message}");
          Sup.LogDebugMessage($" InetSupport: Failed FTPS connecting to {hostname}. Files will not be transferred");
          FTPvalid = false;
        }
        catch (Exception e)
        {
          Sup.LogDebugMessage($" InetSupport: Unknown Exception on FTPS connecting to {hostname}: {e.Message}");
          Sup.LogDebugMessage($" InetSupport: Failed FTPS connecting to {hostname}. Files will not be transferred");
          FTPvalid = false;
          throw;
        }
      }

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (1 by maintainers)

Commits related to this issue

Most upvoted comments

A follow up on running under Mono, I was getting the same safe handle errors on connection and every file operation. I worked around the connection error by using conn.ConnectAysnc().Wait(); instead of just conn.Connect(); - As in #865

I compiled the code myself, and found that in FtpClient_Stream.cs : OpenActiveDataStream() (for connections) and FtpSocketStream.cs : Read(), that commenting out the ar.AsyncWaitHandle.Close(); in each file and just letting the object be disposed at the end of the functions fixed all the safe handle errors, and the FTP now works fine.

Mono support is experimental and only supported via .NET Standard. We do not test with Mono, neither is it in scope now that .NET Core / .NET 5 has been released and Linux support is officially supported using Microsoft SDKs/runtimes.