[CSHARP-4513] Problem with MonitorServer heartbeat threads. Created: 02/Feb/23  Updated: 07/Feb/23  Resolved: 07/Feb/23

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

Type: Bug Priority: Major - P3
Reporter: Semyon Pivovarov Assignee: James Kovacs
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2023-02-02-10-08-20-460.png    
Issue Links:
Duplicate
duplicates CSHARP-3431 Make MongoClient IDisposable to allow... Scheduled
Related
is related to CSHARP-3761 Reimplement SDAM to use a dedicated t... Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary:

When you once created a client and created a connection to one server/cluster the thread that performs heartbeat (that lives in MongoDB.Driver.Core.Servers.MonitorServer class) will be working all the time before the application stop.

Version to reproduce:

From 2.15.0 (thread was added in https://jira.mongodb.org/browse/CSHARP-3761) to the latest (2.19.0).

How to Reproduce:

  1. Create MongoClient with connection to some server/cluster.
  2. Delete MongoClient (MongoClient is out of scope and maybe will be destroyed sometimes when GC will decide)
  3. We don't need Mongo connection, but the heartbeat thread is working. And we can't control it.

Additional Background:

That's how look threads list in our application after some time, there are 300+ threads created by MonitorServer and they never die.

A lot of connections exist which never close.



 Comments   
Comment by James Kovacs [ 07/Feb/23 ]

Hi, Simon,

Thank you for reporting this issue. Unfortunately the fix is not as straightforward as you describe. If you create multiple MongoClient instances using the same (or similar) connection strings (determined by matching ClusterKey), they use the same underlying cluster and connection pools. Disposing one MongoClient instance could break other MongoClient instances. There is an additional race condition that occurs when you dispose of a Cluster and then create a new one with the same ClusterKey.

I have closed this issue as a duplicate of CSHARP-3431, which is an existing ticket to make MongoClient be disposable. Please follow that ticket for updates. Note that this is a breaking change and is planned for the 3.0 release of the driver.

Sincerely,
James

Comment by Esha Bhargava [ 03/Feb/23 ]

semyon@365scores.com Thank you for reporting this issue. We'll look into it and get back to you soon.

Comment by Semyon Pivovarov [ 02/Feb/23 ]

Created PR with suggestions to fix: https://github.com/mongodb/mongo-csharp-driver/pull/1018

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