[CSHARP-2086] Inconsistent MongoClient Connection Behavior- Timeout depending upon connection setup Created: 01/Nov/17  Updated: 09/Feb/22  Resolved: 09/Feb/22

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

Type: Bug Priority: Major - P3
Reporter: Bret Ferrier Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

.Net Full, Windows Server 2012



 Description   

So I have a long running Windows Service that is performing a few dozen tasks each of which read/write to two different mongo servers (one being a 3 member replica set and the other being a single member replica set).

The MongoClients for all processes are being handled by a Singleton. This has been up and running for a year+ with no issues using the following code to acquire a MongoClient.

```
_client = new MongoClient(connstring);
```
Simple Enough.
Now we wanted to try and get some analytics for the queries and changed it to the following;

```
Action<string, DateTime, TimeSpan, bool> _trackIt = null;
var mongoUrl = new MongoUrl(connstring);
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl);
mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
{
if (_trackIt != null)

{ //Code Here for profiling that is never called }

};

_client = new MongoClient(mongoClientSettings);
```

Now with the Example above the Action used for tracking is in fact null as this process isn't collecting any analytics on performance.
I would expect this to perform the same but after a couple of hours of running the tasks without an issue we start getting the following error and have to constantly re-start the service.

```
System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector

{ AllowedLatencyRange = 00:00:00.0150000 }

}. Client view of cluster state is { ClusterId : "8536", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "

{ ClusterId : 8536, EndPoint : "Unspecified/someserver.in.datacenter.com:27017" }

", EndPoint: "Unspecified/someserver.in.datacenter.com:27017", State: "Disconnected", Type: "Unknown" }] }.
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Bindings.WritableServerBinding.GetWriteChannelSource(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.FindAndModifyOperationBase`1.Execute(IWriteBinding binding, CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperation[TResult](IWriteOperation`1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl`1.FindOneAndUpdate[TProjection](FilterDefinition`1 filter, UpdateDefinition`1 update, FindOneAndUpdateOptions`2 options, CancellationToken cancellationToken)
```

We get the error for both servers each erroring out independently. No changes are made to the connection string between the failing and error-less execution path.



 Comments   
Comment by James Kovacs [ 09/Feb/22 ]

ClusterConfigurator defines an Action<ClusterBuilder> delegate to run when a new cluster is created. If the delegate does not execute any code, then it cannot modify the default cluster configuration and thus cannot impact cluster connectivity.

If you encounter a similar issue in the future, we encourage you to reach out to the following channels to help diagnose the issue:

  • Our MongoDB support portal, located at support.mongodb.com
  • Our MongoDB community portal, located here
  • If you are an Atlas customer, you can review your support options by clicking Support in the top menu bar of the Atlas UI
Generated at Wed Feb 07 21:41:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.