|
I have been using C# MongoDB.Driver 2.6.1 very successfully with MongoDB engine v4.2.10 running on a Raspberry Pi 4 (Ubuntu 20.04 64-bit)
However when I upgrade MongoDB.Driver to version 2.11.4, an exception is thrown when constructing a MongoClient.
Here is a simplified version of my GetMongoClient code:
public static MongoClient GetMongoClient(
|
string machineName,
|
string dbName,
|
string username,
|
SecureString password
|
)
|
{
|
var connString = $"mongodb://{machineName}/";
|
var settings = MongoClientSettings.FromConnectionString(connString);
|
if (!String.IsNullOrEmpty(username) && password != null)
|
{
|
var credential = MongoCredential.CreateCredential(dbName, username, password);
|
settings.Credential = credential;
|
}
|
|
Console.WriteLine($"GetMongoClient - created settings: {settings}");
|
Console.WriteLine($" ConnString: {connString}");
|
Console.WriteLine($" HeartbeatTimeout: {settings.HeartbeatTimeout}");
|
Console.WriteLine($" MaxConnectionIdleTime: {settings.MaxConnectionIdleTime}");
|
Console.WriteLine($" MaxConnectionLifeTime: {settings.MaxConnectionLifeTime}");
|
Console.WriteLine($" ServerSelectionTimeout: {settings.ServerSelectionTimeout}");
|
Console.WriteLine($" SocketTimeout: {settings.SocketTimeout}");
|
Console.WriteLine($" WaitQueueTimeout: {settings.WaitQueueTimeout}");
|
return new MongoClient(settings); // <----- EXCEPTION THROWN HERE
|
}
|
Here is the debugging output produced before the exception is thrown:
GetMongoClient - created settings: ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{}};GuidRepresentation=CSharpLegacy;HeartbeatInterval=00:00:10;HeartbeatTimeout=-00:00:00.0010000;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadConcern={ };ReadPreference={ Mode : Primary };ReplicaSetName=;RetryReads=TrueRetryWrites=TrueLocalThreshold=00:00:00.0150000;Servers=127.0.0.1:27017;ServerSelectionTimeout=00:00:30;SocketTimeout=00:00:00;SslSettings={CheckCertificateRevocation=False,EnabledProtocols=Tls, Tls11, Tls12};Tls=False;TlsInsecure=False;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern={ };
|
ConnString: mongodb://127.0.0.1/
|
HeartbeatTimeout: -00:00:00.0010000
|
MaxConnectionIdleTime: 00:10:00
|
MaxConnectionLifeTime: 00:30:00
|
ServerSelectionTimeout: 00:00:30
|
SocketTimeout: 00:00:00
|
WaitQueueTimeout: 00:02:00
|
And here are the exception details:
ArgumentOutOfRangeException: Value is not greater than zero: 00:00:00.
|
Parameter name: maxLifeTime
|
at MongoDB.Driver.Core.Misc.Ensure.IsGreaterThanZero(TimeSpan value, String paramName)
|
at MongoDB.Driver.Core.Configuration.ConnectionSettings..ctor(Optional`1 authenticatorFactories, Optional`1 compressors, Optional`1 maxIdleTime, Optional`1 maxLifeTime, Optional`1 applicationName)
|
at MongoDB.Driver.Core.Configuration.ConnectionSettings.With(Optional`1 authenticatorFactories, Optional`1 compressors, Optional`1 maxIdleTime, Optional`1 maxLifeTime, Optional`1 applicationName)
|
at MongoDB.Driver.ClusterRegistry.ConfigureConnection(ConnectionSettings settings, ClusterKey clusterKey)
|
at MongoDB.Driver.Core.Configuration.ClusterBuilder.ConfigureConnection(Func`2 configurator)
|
at MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey)
|
at MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey)
|
at MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings)
|
at MyNamespace.GetMongoClient(String machineName, String dbName, String username, SecureString password)
|
...
|
As far as I can see the 'maxLifeTime' parameter referred to corresponds to the MongoClientSettings.MaxConnectionLifeTime property, which as you can see from my logging is set to 30 minutes.
I have also experimented with explicitly settings various other time-related properties, but none have made any difference.
This only seems to be a problem when running on Linux, as the same code works perfectly using the upgraded driver on Windows.
|
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.
|
|
Hi, Ted,
Thank you for your follow-up. Publishing on netcoreapp2.1 and copying to the ARM64 EC2 instance does in fact produce the same exception as you noted (ArgumentOutOfRangeException for maxLifeTime). If we instead publish to netcoreapp3.1 (with the same above-mentioned changes used for net5.0) and copy to the ARM64 EC2 instance, the application runs successfully.
~/MongoClientTest-Published-3.1$ ./MongoClientTest.WebApp
|
Inf: Starting host on URLs:
|
http://127.0.0.1:50010/
|
Inf: Running application
|
Deb: GetMongoClient - created settings: ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{}};GuidRepresentation=CSharpLegacy;HeartbeatInterval=00:00:10;HeartbeatTimeout=-00:00:00.0010000;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadConcern={ };ReadPreference={ Mode : Primary };ReplicaSetName=;RetryReads=TrueRetryWrites=TrueLocalThreshold=00:00:00.0150000;Servers=localhost:27017;ServerSelectionTimeout=00:00:30;SocketTimeout=00:00:00;SslSettings={CheckCertificateRevocation=False,EnabledProtocols=Tls, Tls11, Tls12};Tls=False;TlsInsecure=False;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern={ };
|
Deb: ConnString: mongodb://localhost/
|
Deb: HeartbeatTimeout: -00:00:00.0010000
|
Deb: MaxConnectionIdleTime: 00:10:00
|
Deb: MaxConnectionLifeTime: 00:30:00
|
Deb: ServerSelectionTimeout: 00:00:30
|
Deb: SocketTimeout: 00:00:00
|
Deb: WaitQueueTimeout: 00:02:00
|
Gen: Initialising all MongoDB stores...'
|
Gen: Accessing store: AccountStore
|
Gen: Sucessfully accessed store: AccountStore
|
Hosting environment: Production
|
Content root path: /home/ubuntu/MongoClientTest-Published-3.1
|
Now listening on: http://127.0.0.1:50010
|
Application started. Press Ctrl+C to shut down.
|
To summarize our test scenarios so far, your application with the .NET/C# MongoDB Driver v2.11.4 works on:
- .NET Core 2.1 on Windows x64 and OSX x64
- .NET Core 3.1 on Windows x64, OSX x64, and Ubuntu 20.04 ARM64
- .NET 5.0 on Windows x64, OSX x64, and Ubuntu 20.04 ARM64
Your application fails on .NET Core 2.1 on Ubuntu 20.04 ARM64. As well building on .NET Core 2.1 on Ubuntu 20.04 ARM64 also fails with Roslyn issue#26505.
This is very suggestive of a bug in .NET Core 2.1 on ARM64 rather than a bug in the .NET/C# MongoDB driver. Given that .NET Core 2.1 will reach end-of-life on August 21, 2021, we recommend upgrading your application to .NET Core 3.1 or .NET 5.0 to work around this issue.
Sincerely,
James
|
|
Hi James,
Thanks for getting back to me.
Just to be clear, I have never built the application on Ubuntu or the rPi4 - it is built on a Windows machine using the command-line I mentioned previously and then deployed by copying the published output onto the Raspberry Pi 4.
dotnet publish -r linux-arm64 -f netcoreapp2.1
Could you let me know if you are able to build it in this way and whether you can reproduce the issue in that way?
Thanks,
Ted
|
|
Hi, Ted,
Thank you for your patience as we investigated. Attempting to build your provided repro on an ARM64 EC2 instance Ubuntu 20.04 resulted in the following error:
~/MongoClientTest$ dotnet restore && dotnet build
|
Restore completed in 3.2 sec for /home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj.
|
Microsoft (R) Build Engine version 16.2.37902+b5aaefc9f for .NET Core
|
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
Restore completed in 220.22 ms for /home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj.
|
/home/ubuntu/dotnet/sdk/2.1.812/Roslyn/Microsoft.CSharp.Core.targets(59,5): error MSB6006: "csc.dll" exited with code 139. [/home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj]
|
|
Build FAILED.
|
|
/home/ubuntu/dotnet/sdk/2.1.812/Roslyn/Microsoft.CSharp.Core.targets(59,5): error MSB6006: "csc.dll" exited with code 139. [/home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj]
|
0 Warning(s)
|
1 Error(s)
|
|
Time Elapsed 00:00:18.72
|
This appears to be Roslyn issue#26505.
We upgraded to .NET 5.0, which required the following minor changes:
- Switching the TFM to net5.0
- Commenting out the package reference to Microsoft.AspNetCore.All (as it is already implicitly included)
- Changing Startup.cs from services.AddMvc() to service.AddMvc(options => options.EnableEndpointRouting = false)
With these minor changes, we are able to build:
~/MongoClientTest$ dotnet restore && dotnet build
|
Determining projects to restore...
|
All projects are up-to-date for restore.
|
Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
|
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
Determining projects to restore...
|
All projects are up-to-date for restore.
|
MongoClientTest.WebApp -> /home/ubuntu/MongoClientTest/bin/linux-arm64/Debug/net5.0/MongoClientTest.WebApp.dll
|
|
Build succeeded.
|
0 Warning(s)
|
0 Error(s)
|
|
Time Elapsed 00:00:02.93
|
We are then able to run the application and it successfully starts up. There are some minor warnings about ASP.NET-related deprecations, but a MongoClient is able to successfully connect to the mongod running on localhost:27017. Notably we do not observe the ArgumentOutOfRangeException for the maxLifeTime parameter:
~/MongoClientTest$ dotnet run
|
/home/ubuntu/MongoClientTest/Startup.cs(38,56): warning CS0618: 'IHostingEnvironment' is obsolete: 'This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.' [/home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj]
|
/home/ubuntu/MongoClientTest/Startup.cs(34,42): warning CS0618: 'CompatibilityVersion.Version_2_1' is obsolete: 'This CompatibilityVersion value is obsolete. The recommended alternatives are Version_3_0 or later.' [/home/ubuntu/MongoClientTest/MongoClientTest.WebApp.csproj]
|
Inf: Starting host on URLs:
|
http://127.0.0.1:50010/
|
Inf: Running application
|
Deb: GetMongoClient - created settings: ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{}};GuidRepresentation=CSharpLegacy;HeartbeatInterval=00:00:10;HeartbeatTimeout=-00:00:00.0010000;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadConcern={ };ReadPreference={ Mode : Primary };ReplicaSetName=;RetryReads=TrueRetryWrites=TrueLocalThreshold=00:00:00.0150000;Servers=localhost:27017;ServerSelectionTimeout=00:00:30;SocketTimeout=00:00:00;SslSettings={CheckCertificateRevocation=False,EnabledProtocols=Tls, Tls11, Tls12};Tls=False;TlsInsecure=False;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern={ };
|
Deb: ConnString: mongodb://localhost/
|
Deb: HeartbeatTimeout: -00:00:00.0010000
|
Deb: MaxConnectionIdleTime: 00:10:00
|
Deb: MaxConnectionLifeTime: 00:30:00
|
Deb: ServerSelectionTimeout: 00:00:30
|
Deb: SocketTimeout: 00:00:00
|
Deb: WaitQueueTimeout: 00:02:00
|
Gen: Initialising all MongoDB stores...'
|
Gen: Accessing store: AccountStore
|
Gen: Sucessfully accessed store: AccountStore
|
Hosting environment: Production
|
Content root path: /home/ubuntu/MongoClientTest
|
Now listening on: http://127.0.0.1:50010
|
Application started. Press Ctrl+C to shut down.
|
Our current hypotheses are that this is either a .NET Core 2.1 on ARM64 issue or possibly an issue specific to Raspberry Pi 4. Please let us know if you are able to reproduce the same behaviour with .NET 5.0 on an ARM64 EC2 instance as that would be most helpful in our further investigations.
Sincerely,
James
|
|
Hi, Ted,
Thank you for providing the repro. We are investigating this further now. We appreciate your patience.
Sincerely,
James
|
|
Hi is there any update on this issue?
|
|
Hi James,
Thanks very much for looking into this for me. The application I'm seeing this in is an ASP.NET Core (2.1) WebApp
I've put together a repro - see attached MongoClientTest.zip - which I'm running on:
.NET Core 2.1
OS: Ubuntu 20.04.1 LTS (Focal Fossa)
Device: Raspberry Pi 4 ARM64
I publish it using:
dotnet publish -c Debug -r linux-arm64 -f netcoreapp2.1
Then copy the publish folder to my device, and run it with:
dotnet MongoClientTest.WebApp.dll
For additional information, when I run a simple .netcore2.1 console app with the exact same code as you used (literally just the Main and GetMongoClient methods) on my platform, I don't get the same exception thrown, however the MongoClient constructor blocks indefinitely!
|
|
Hi, Ted,
Thank you for reporting this issue with the .NET/C# driver on Ubuntu 20.04 running on arm64. We understand that creating a MongoClient instance using the provided GetMongoClient method results in the observed stack trace.
We attempted to reproduce the issue as follows using your unmodified GetMongoClient method:
static void Main(string[] args)
|
{
|
var client = GetMongoClient("localhost", "test", null, null);
|
Console.WriteLine(client != null);
|
Console.WriteLine("Done");
|
}
|
The output on Ubuntu 20.04 arm64 was:
$ dotnet run
|
GetMongoClient - created settings: ConnectionMode=Automatic;ConnectTimeout=00:00:30;Credentials={{}};GuidRepresentation=CSharpLegacy;HeartbeatInterval=00:00:10;HeartbeatTimeout=-00:00:00.0010000;IPv6=False;MaxConnectionIdleTime=00:10:00;MaxConnectionLifeTime=00:30:00;MaxConnectionPoolSize=100;MinConnectionPoolSize=0;ReadConcern={ };ReadPreference={ Mode : Primary };ReplicaSetName=;RetryReads=TrueRetryWrites=TrueLocalThreshold=00:00:00.0150000;Servers=localhost:27017;ServerSelectionTimeout=00:00:30;SocketTimeout=00:00:00;SslSettings={CheckCertificateRevocation=False,EnabledProtocols=Tls, Tls11, Tls12};Tls=False;TlsInsecure=False;WaitQueueSize=500;WaitQueueTimeout=00:02:00WriteConcern={ };
|
ConnString: mongodb://localhost/
|
HeartbeatTimeout: -00:00:00.0010000
|
MaxConnectionIdleTime: 00:10:00
|
MaxConnectionLifeTime: 00:30:00
|
ServerSelectionTimeout: 00:00:30
|
SocketTimeout: 00:00:00
|
WaitQueueTimeout: 00:02:00
|
True
|
Done
|
The MongoClient was created successfully and did not throw the ArgumentOutOfRangeException that you observed. We verified on .NET Core 3.1 and .NET 5.0.
Walking through the stack trace, we definitely assert that maxLifeTime (AKA MaxConnectionLifeTime) has to be greater than zero and the default is 30 minutes. The output from your settings clearly shows that MaxConnectionLifeTime is 30 minutes. It is unclear how this assertion was triggered.
We would be happy to investigate further, but have been unable to reproduce the issue with the code provided. Please provide a self-contained repro demonstrating the problem and include .NET Core version, OS version, processor type, and any other relevant information.
Sincerely,
James
|
Generated at Wed Feb 07 21:44:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.