Timeout when reaching to server after certain time of inactivity (CompositeServerSelector Error) with MongoDb C# Driver

Hi, we are currently developing a Web Application using C# and Blazor (Dot net 8) with Mudblazor (6.11.1). We have a page which requests data from a MongoDb Server (Mongo Atlas ReplicaSet) and displays it in our application. For this we are using the MongoDb Driver 2.22.0. The application consists of the following:

The application is hosted in Azure in the App Services Resource. The error prompts when the application tries to request the data after certain time of inactivity and in the console shows the following error:

[2023-12-12T21:55:37.934Z] Error: System.TimeoutException: A timeout occurred after 3000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode : Secondary } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/testcluster-shard-00-00.b3lkr.mongodb.net:27017" }", EndPoint: "Unspecified/testcluster-shard-00-00.b3lkr.mongodb.net:27017", ReasonChanged: "ServerInitialDescription", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", LastHeartbeatTimestamp: null, LastUpdateTimestamp: "2023-12-12T21:55:34.5974442Z" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/testcluster-shard-00-01.b3lkr.mongodb.net:27017" }", EndPoint: "Unspecified/testcluster-shard-00-01.b3lkr.mongodb.net:27017", ReasonChanged: "ServerInitialDescription", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", LastHeartbeatTimestamp: null, LastUpdateTimestamp: "2023-12-12T21:55:34.5993470Z" }, { ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/testcluster-shard-00-02.b3lkr.mongodb.net:27017" }", EndPoint: "Unspecified/testcluster-shard-00-02.b3lkr.mongodb.net:27017", ReasonChanged: "ServerInitialDescription", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", LastHeartbeatTimestamp: null, LastUpdateTimestamp: "2023-12-12T21:55:34.6002657Z" }] }.
   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.WaitForDescriptionChangedAsync(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Clusters.IClusterExtensions.SelectServerAndPinIfNeededAsync(ICluster cluster, ICoreSessionHandle session, IServerSelector selector, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Bindings.ReadPreferenceBinding.GetReadChannelSourceAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AggregateOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition`2 pipeline, AggregateOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)

It takes a while until the error is prompted in the web application and after the reloading it works fine (fast). It seems that when the server does not have activity and a request is made it gives the error but after that ping to the server it kind of becomes active and the requests made work fine.

We tried to replicate the error localy ( we are connected through a vpn) and it works fine. But when it is deployed in App Services, it is working intermittently alway prompting the error in the first attemp after inactivity.

The requests are made through a pipeline using the MongoDb Driver. The connection string is the following:

mongodb+srv://xxxxxxxxx:yyyyyyyyy@zzzzzzzz.mongodb.net/retryWrites=true&w=majority&serverSelectionTimeoutMS=3000&directConnection=true`

Any suggestions or another option to diagnose this?

1 Like

After reviewing the post I noticed that the connection string in the question is wrong.

We are using the following connection string:

mongodb+srv://xxxxxxxxx:yyyyyyyyy@zzzzzzzz.mongodb.net/retryWrites=true&w=majority&serverSelectionTimeoutMS=3000

We are still having the error, does anyone have a suggestion or other path to take?