0

I have an application based using .Net Core 2.2 that is connecting to MondoDb cluster V3.6 in Atlas. The application is hosted in Azure as a Linux Docker container. The app is using MongoDB .Net driver 2.7.3. The app periodically (once in a couple minutes) receives the following timeout exceptions:

System.TimeoutException at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException

and

System.TimeoutException at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync

The mongo client instance is configured according to the MongoDb docs, i.e.

var url = MongoUrl.Create("mongodb+srv://user:password@cluster.gcp.mongodb.net/?authSource=admin&retryWrites=true&ssl=true");
        var clientSettings = MongoClientSettings.FromUrl(url);
        clientSettings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };

        void SocketConfigurator(Socket s) => s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

        clientSettings.ClusterConfigurator = builder =>
            builder.ConfigureTcp(tcp => tcp.With(socketConfigurator: (Action<Socket>)SocketConfigurator));

        return new MongoClient(clientSettings);

I checked number of SO questions including MongoDB C# 2.0 TimeoutException and SocketTimeout with opened connection in MongoDB but the suggestions seem to be either outdated (reported as fixed in the current version of driver) or don't have permanent positive effect (setting timeouts in the connection string i.e. connectTimeoutMS=90000&socketTimeoutMS=90000&maxIdleTimeMS=90000). The second one (setting tcp_keepalive_time) seems to be not applicable to a docker container in Azure. Please help.

AndrewG
  • 186
  • 4
  • Did you manage to fix this? I am getting this regularly – Paul Dec 03 '20 at 22:59
  • As stated below, the reason was a network issue. Once we moved Atlas db from Google host to Azure host, the timeouts of sockets open operation disappeared. Still not sure which side caused the issue. – AndrewG Dec 07 '21 at 12:30

1 Answers1

-1

Have you tried setting like this:

var client = new MongoClient(new MongoClientSettings
{
       Server = new MongoServerAddress("xxxx"),
       ClusterConfigurator = builder =>
       {
             builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(10)));
       }
});
Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • Well, the reason was a network issue. Once we moved Atlas db from Google host to Azure host, the timeouts of sockets open operation disappeared. Still not sure which side caused the issue. – AndrewG Mar 21 '19 at 13:02