drizin
drizin

Reputation: 1975

Azure Cosmos DB - intermittent MongoConnectionException / IOException / SocketException

I'm using Azure Cosmos DB 4.0 with MongoDB C# Driver 2.10.4.

Most of the times the queries work fine, but I'm getting intermittent errors like this:

MongoDB.Driver.MongoConnectionException: An exception occurred while sending a message to the server. System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.BeginSend(... at System.Net.Sockets.NetworkStream.BeginWrite --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginWrite at System.Net.Security._SslStream.StartWriting at System.Net.Security._SslStream.ProcessWrite at System.Net.Security._SslStream.BeginWrite

When that error happens the call takes 10-25 seconds before failing.

I'm building the MongoClient with new MongoClient(MongoClientSettings.FromConnectionString(cnstr)) and I was using the connectionstring with these arguments ?ssl=true&replicaSet=globaldb&retrywrites=false.

I tried with retryWrites=true (as per Azure Support suggestion) but that didn't help.

I tried different settings and that didn't work either (connect=direct, maxIdleTimeMS=30000, serverSelectionTimeout=5000ms, socketTimeout=10000ms).

What's causing those exceptions?

Upvotes: 0

Views: 1008

Answers (1)

drizin
drizin

Reputation: 1975

The fix was to set/force TLS 1.2 (based on this Microsoft document):

//return new MongoClient(connectionString);
var settings = MongoClientSettings.FromConnectionString(connectionString);
settings.SslSettings = new SslSettings()
{
    EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12
};
return new MongoClient(settings);

Looks like although my connection string had ssl=true, it wasn't enough to work on some servers (the error is intermittent). The same underlying error can usually be fixed by forcing TLS 1.2 so I assumed that in Mongo it could be the same issue - and it really fixed the problem.

Upvotes: 0

Related Questions