Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3117

Fix timeout connecting to server on Linux

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.11.0
    • Affects Version/s: 2.11.0-beta1
    • Component/s: Connectivity
    • None
    • Environment:
      Docker debian

      Hi,

      I'm in the process of updating several applications to .NET Core and after updating the MongoDB driver to version 2.11.0-beta1 our deploys started to fail.

      After quite a lot of trial and error I found a StackOverflow question from 19 days ago detailing the same error we were getting:

      https://stackoverflow.com/questions/61700446/cant-connect-to-mongodb-in-docker-container-via-hostname

      The only answer mentions that manually resolving the server hostnames fixes the error. I've tried and indeed fixed our deploy issues.

      The same answer mentions that this issue was fixed a long time ago, is this a regression?

       Thanks,

      Jose

      Error:

      ---> System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector\{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatncyRange = 00:00:00.0150000 } }. Client view of cluster state is \{ ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/<SERVER HOSTNAME>:27017" }", EndPoint: "Unspecified/<SERVER HOSTNAME>:27017", ReasonChanged: "Heartbeat", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Diver.MongoConnectionException: An exception occurred while opening a connection to the server.
       ---> System.PlatformNotSupportedException: Sockets on this platform are invalid for use after a failed connection attempt.
       at System.Net.Sockets.Socket.ThrowMultiConnectNotSupported()
       at System.Net.Sockets.Socket.BeginConnect(String host, Int32 port, AsyncCallback requestCallback, Object state)
       at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
       at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
       at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
      

      Workaround:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      var servers = new List<MongoServerAddress>();
      
      // Resolve hostnames
       foreach (var server in mongoDbSettings.Servers)
       {
          // hostname:port
          var parts = server.Split(':');
      
          var address = Dns.GetHostAddresses(parts[0])
              .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
      
          servers.Add(new MongoServerAddress(address.ToString(), int.Parse(parts[1])));
       }
      
      var mongoClientSettings = new MongoClientSettings
       {
          Servers = servers
       };
      

            Assignee:
            vincent.kam@mongodb.com Vincent Kam (Inactive)
            Reporter:
            jose.massada@farfetch.com José Massada
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: