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

Memory leak when closure/object is used for ClusterConfigurator

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Unknown Unknown
    • None
    • None
    • None
    • None

    Description

      Summary

      When using closure or new object and passing its method to ClusterConfigurator property it causes memory leak because every MongoClient creation leads to hidden threads being created for each client creation.

      Nuget MongoDB.Driver version is 2.17.1.

      How to Reproduce

      • Create web application
      • for each web request create new MongoClient which uses this configuration for 
        ClusterConfigurator property.
         

          public class Test
          {
              public Test()
              {
              }        public void ClusterConfiguration(ClusterBuilder b)
              {
              }
          }
       
      ...
       
      var mongoConnectionUrl = new MongoUrl(mongoConnectionString);
      var mongoClientSettings =MongoClientSettings.FromUrl(mongoConnectionUrl);
      var c = new Test();
      mongoClientSettings.ClusterConfigurator = c.ClusterConfiguration; 

      Additional Background

      I think that problem is caused with logic which uses this property to check equality of cluster keys and since we use new object every time when setting this property then equality fails and clusterKey is created every time which leads to RoundTripTimeMonitor being created every time with new thread.

      After some time many threads is created and it can lead to application out of memory crash.

       

      I understand that it is used to calculate equality but I think that simple assignment of action or method of newly created object should behave same and should not lead to different behavior which in the end leads to out of memory problem.

      Developer cannot know that he can assign only action and cannot assign delegate of newly created object or closure. It is very hidden behavior which is hard to analyze.

      What do you think?

       

      I think that it is similar to issue https://jira.mongodb.org/browse/CSHARP-1947

       

      Thanks

      Attachments

        Activity

          People

            Unassigned Unassigned
            dusan.hudecek01@gmail.com Dusan Hudecek
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: