[CSHARP-4314] Memory leak when closure/object is used for ClusterConfigurator Created: 08/Sep/22  Updated: 27/Oct/23  Resolved: 12/Sep/22

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Dusan Hudecek Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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



 Comments   
Comment by Dusan Hudecek [ 12/Sep/22 ]

Hi @Boris Dogadov,

now i can see that information in documentation so bug is not bug anymore.
Still I woud suggest at least to add some info to intellisence for that property or something like that because from C# point of view developer (me) adds action or point to method of object or closure and does not expect it to be a problem I would say. I think it would help developers to quickly find problem if they would do what I did. Just suggestion.

thanks for your help.

 

 

Comment by Boris Dogadov [ 09/Sep/22 ]

Hi dusan.hudecek01@gmail.com, thank you for your question.

Yes, it is a known behavior, documented here: https://mongodb.github.io/mongo-csharp-driver/2.17/reference/driver/connecting/
In order to reuse the underlying cluster instance, ClusterConfigurator address has to be the same.

Generated at Wed Feb 07 21:47:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.