[CSHARP-2679] Connection aborted in MongoDB Created: 06/Aug/19  Updated: 27/Oct/23  Resolved: 26/Aug/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.8.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: daniel friedmann Assignee: Vincent Kam (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi all,

We are working on automatic upgrade tool to migrate data from Mongo 2.4 to Mongo 4.0 and we are using Mongo driver version 2.8.1 with  .net core version 2.1.

During the upgrade process We are connecting via Mongo client to  Mongo Server 2.4, then shutting  down the service ,upgrading  the Mongo version  to 2.6  starting it and then stopping it upgrading to 3.0 and etc.

During this process We have a problem of disconnection of the mongo driver as result of shutting down the Mongo services which seems to stuck the Mongo driver and every command we are trying to execute for example ListDatabases from our mongo client gets the following exception:

System.AggregateException: One or more errors occurred. (An exception occurred while receiving a message from the server.) ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine

   — End of inner exception stack trace —

   at MongoDB.Driver.Core.Misc.StreamExtensionMethods.ReadBytesAsync(Stream stream, Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync()

   — End of inner exception stack trace —

   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync()

   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveBufferAsync(Int32 responseTo, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessageAsync(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocolAsync(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)

   at MongoDB.Driver.Core.Operations.ReadCommandOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)

   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)

   at MongoDB.Driver.MongoDatabaseImpl.ExecuteReadOperationAsync[T](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)

   at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)

   — End of inner exception stack trace —

   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)

   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout)

 

 

Thank you.

Daniel.



 Comments   
Comment by Vincent Kam (Inactive) [ 26/Aug/19 ]

Hi danielfr@riscogroup.com,

Thank you for filing a JIRA ticket! A socket exception is expected when shutting down your `mongod` instances: using driver version 2.9.1 may also help due to the addition of the retryable reads feature, which is enabled by default.

Kind regards,
Vincent

Comment by daniel friedmann [ 25/Aug/19 ]

Hey all.

We solve the issue with a workaround.

we set the MaxConnectionIdleTime property to 500 milliseconds and it force the driver to reconnect.

Comment by daniel friedmann [ 15/Aug/19 ]

 

After every 2 upgrades we get the listDatabases exception,  and It happens for the first time after we restore the db.

 

Below you can find the function that we are using which the line that gets the exception (marked in red).

After trying debug the mongo driver we see that the socket is not available. we saw the issue is in class BinaryConnection line 314:

_stream.ReadBytes(messageSizeBytes, 0, 4, _backgroundTaskCancellationToken);

 

       public bool MongoDump(string arguments, string exportPath)

       

{             var finalArgument = $"\{GetLocalMongoShellAuthenticationArgument()}

{arguments} --out {exportPath}";

            _log.Info($"Performing MongoDump with arguments {arguments}");

            using (var process = CreateMongoProcess(MongoConstants.MongoDumpFileName, finalArgument))

           

{                 process.Start();                 process.WaitForExit();                   return process.ExitCode == 0;             }

        }

 

        public bool MongoRestore(bool isLocalRestore, bool authenticationRequired, string arguments, string importPath)

        {

            string finalArgument;

 

            if (isLocalRestore)

            {                 var authentication = authenticationRequired ? GetLocalMongoShellAuthenticationArgument() : string.Empty;                 finalArgument = $"\{authentication} {arguments} {importPath}";

            }

            else

            {                 var authentication = authenticationRequired ? GetRemoteMongoShellAuthenticationArgument() : string.Empty;                 finalArgument = $"\{authentication} {importPath}";

            }

 

            _log.Info($"Performing MongoRestore with arguments {arguments}");

            using (var process = CreateMongoProcess(MongoConstants.MongoRestoreFileName, finalArgument))

            {                 process.Start();                 process.WaitForExit();                   return process.ExitCode == 0;             }

        }

  

 

 

var mongoClient = new MongoClient(mongoCredentials);

 

using (var databaseCursor = mongoClient.ListDatabases())

               

{          }

 

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