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

connecting to mongo instance 300+ times result with error: System.outofmemory exception

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.15.0, 2.15.1, 2.16.0, 2.16.1
    • Component/s: Connectivity
    • None

      Summary

      I have a C# program, basically, it connects to all mongo instances, and get the version information. to ensure that the mongodb version is as expected. The program works smoothly for driver version 2.14.1 for long time. Recently, I upgrade Mongo C# driver version from 2.14.1 to 2.15.0 and above, and program throw error below:

       

      processing:    328 of 452
      error: System.OutOfMemoryException:  type is “System.OutOfMemoryException”
         at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
         at System.Threading.Thread.Start(StackCrawlMark& stackMark)
         at System.Threading.Thread.Start()
         at MongoDB.Driver.Core.Servers.RoundTripTimeMonitor.Start()
         at MongoDB.Driver.Core.Servers.ServerMonitor.Initialize()
         at MongoDB.Driver.Core.Servers.DefaultServer.InitializeSubClass()
         at MongoDB.Driver.Core.Servers.Server.Initialize()
         at MongoDB.Driver.Core.Clusters.MultiServerCluster.Initialize()
         at MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey)
         at MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey)
         at MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings)
         at MongoDB.Driver.MongoClient..ctor(MongoUrl url)
         at Ctrip.DBConfig.Business.MongoCheckVersion.check()

       

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      2.15.0,  2.15.1, 2.16.0 and 2.16.1  work smoothly for 2.14.1 and below.

      How to Reproduce

       

                  List<MongoClusterInfo> MongoClusterList = SQLMonitorV3.getMongoClusterList();
                  for (int i = 0; i < MongoClusterList.Count; i++)
                  {
                      string cluster_name = MongoClusterList[i].cluster_name;
                      string env_type = MongoClusterList[i].env_type;
                      string cluster_type = MongoClusterList[i].cluster_type;
                      int status = MongoClusterList[i].status;
                      int is_dr = MongoClusterList[i].is_dr;

                      
                      string user = "username";
                      string pwd = "password";
                      string connString = "mongodb://" + user + ":" + pwd + "@";

                      Console.WriteLine("processing:" + "\t" + i.ToString() + " of " + MongoClusterList.Count.ToString());

                      if (cluster_type.Equals("sharding"))
                      {
                          List<MongoInstanceInfo> MongoInstanceList = SQLMonitorV3.getMongoInstanceList(cluster_name + "mongos");
                          for (int j = 0; j < MongoInstanceList.Count; j++)
                         

      {                         string mongos_ip = MongoInstanceList[j].ip_business;                         string mongos_port = MongoInstanceList[j].port;                         connString += mongos_ip + ":" + mongos_port + ",";                     }

                          connString = connString.Substring(0, connString.Length - 1);
                          connString += "/?authSource=admin";

                      }
                      else
                      {

                          List<MongoInstanceInfo> MongoInstanceList = SQLMonitorV3.getMongoInstanceList(cluster_name);
                          for (int j = 0; j < MongoInstanceList.Count; j++)
                         

      {                         string machine_name = MongoInstanceList[j].machine_name;                         string ip_business = MongoInstanceList[j].ip_business;                         string port = MongoInstanceList[j].port;                         string role = MongoInstanceList[j].role;                         string mongodb_version = MongoInstanceList[j].mongodb_version;                         if (role.ToLower().Equals("arbiter")) continue;                         if (role.ToLower().Equals("mongos")) continue;                         connString += ip_business + ":" + port + ",";                     }

                          connString = connString.Substring(0, connString.Length - 1);
                          connString += "/admin" + "?replicaSet=" + cluster_name;
                      }
                      
                      try
                      {
                          var mongourl = new MongoUrl(connString);
                          MongoClient client = new MongoClient(mongourl);
                          var db = client.GetDatabase("admin");
                          BsonDocument result = db.RunCommand<BsonDocument>(new BsonDocument("buildInfo", 1));
                          string version = (string)result.GetValue("version");
                          string allocator = (string)result.GetValue("allocator");

                          if (!(version.Equals("4.0.19")  || 
                              version.Equals("4.2.17") ||
                              version.Equals("4.2.19") ||
                              version.StartsWith("4.2.") ||
                              version.Equals("4.2.18")))
                         

      {  Console.WriteLine("expected");                         continue;                     }

                          if (!allocator.Equals("tcmalloc"))
                         

      {  Console.WriteLine("expected");                     }

                          
                      }
                      catch (Exception e)
                     

      {                     Console.WriteLine("error: "  + e.ToString());                     break;                 }

                  }//end of for

       

      Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

       

      1. ensure we have more than 400 mongo instances to check.
      2. Run the above program, it will throw out error when in around iteration 330.
      3. Only the iteration number counts. I skip some iterations, and it will hit error later
      4. Note, once I revert driver back to 2.14.1. it works fine.

       

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

            Assignee:
            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            Reporter:
            rgyu@trip.com rgyu N/A
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: