[CSHARP-3117] Fix timeout connecting to server on Linux Created: 28/May/20  Updated: 28/Oct/23  Resolved: 09/Jun/20

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 2.11.0-beta1
Fix Version/s: 2.11.0

Type: Bug Priority: Major - P3
Reporter: José Massada Assignee: Vincent Kam (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Docker debian


Issue Links:
Duplicate
is duplicated by CSHARP-3126 [Cosmos DB] Current beta breaks conne... Closed
Related
is related to CSHARP-2953 Check TcpStreamFactory methods when t... Closed

 Description   

Hi,

I'm in the process of updating several applications to .NET Core and after updating the MongoDB driver to version 2.11.0-beta1 our deploys started to fail.

After quite a lot of trial and error I found a StackOverflow question from 19 days ago detailing the same error we were getting:

https://stackoverflow.com/questions/61700446/cant-connect-to-mongodb-in-docker-container-via-hostname

The only answer mentions that manually resolving the server hostnames fixes the error. I've tried and indeed fixed our deploy issues.

The same answer mentions that this issue was fixed a long time ago, is this a regression?

 Thanks,

Jose

Error:

---> System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector\{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatncyRange = 00:00:00.0150000 } }. Client view of cluster state is \{ ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/<SERVER HOSTNAME>:27017" }", EndPoint: "Unspecified/<SERVER HOSTNAME>:27017", ReasonChanged: "Heartbeat", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Diver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.PlatformNotSupportedException: Sockets on this platform are invalid for use after a failed connection attempt.
 at System.Net.Sockets.Socket.ThrowMultiConnectNotSupported()
 at System.Net.Sockets.Socket.BeginConnect(String host, Int32 port, AsyncCallback requestCallback, Object state)
 at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)

Workaround:

var servers = new List<MongoServerAddress>();
 
// Resolve hostnames
 foreach (var server in mongoDbSettings.Servers)
 {
    // hostname:port
    var parts = server.Split(':');
 
    var address = Dns.GetHostAddresses(parts[0])
        .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
 
    servers.Add(new MongoServerAddress(address.ToString(), int.Parse(parts[1])));
 }
 
var mongoClientSettings = new MongoClientSettings
 {
    Servers = servers
 };



 Comments   
Comment by Vincent Kam (Inactive) [ 09/Jun/20 ]

This fix will be included as part of 2.11.0-beta2! Thank you so much to all the reporters for bringing this to our attention! 

Comment by Githook User [ 09/Jun/20 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-3117: Fix timeout connecting to server on Linux
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/6f4b11989f24b7506fbc91ce2acb7369d13eca98

Comment by Vincent Kam (Inactive) [ 08/Jun/20 ]

https://github.com/vincentkam/mongo-csharp-driver/pull/117

Comment by José Massada [ 29/May/20 ]

Found this:
https://github.com/dotnet/runtime/issues/24917

According to that issue, enabling KeepAlive breaks Connect, which is being enabled here:
https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs#L280

Comment by José Massada [ 28/May/20 ]

https://github.com/dotnet/runtime/issues/16263#issuecomment-207553272

 

It looks like dotnet core can't connect by host in linux,

Comment by José Massada [ 28/May/20 ]

Found this in the source code:
https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs#L49

For NETSTANDARD1_5 || NETSTANDARD1_6 the hostname is being resolved but not for .NET standard 2.0

Comment by José Massada [ 28/May/20 ]

Forgot to mention that we only see the issue within a linux docker container.

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