[CSHARP-3990] Use `throw;` instead of `throw ex;` when re-throwing an unwrapped exception in BinaryConnection Created: 08/Dec/21  Updated: 28/Oct/23  Resolved: 04/Jan/22

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

Type: Bug Priority: Unknown
Reporter: Анатолий Буранов Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

Assume a program contains an error in CommandSucceededEvent event handler:

using System;
using MongoDB.Driver;
using MongoDB.Driver.Core.Events;
 
namespace MongoDbStackBug
{
    class Program
    {
        static void Main(string[] args)
        {
            var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017/test");
            mongoClientSettings.ClusterConfigurator = cc =>
            {
                cc.Subscribe<CommandSucceededEvent>(cmd =>
                {
                    Console.WriteLine($"Command duration {cmd.Duration}");
 
                    // NRE here
                    ((object)null).ToString();
                });
            };
 
            var mongoClient = new MongoClient(mongoClientSettings);
 
            mongoClient.GetDatabase("test").ListCollections();
        }
    }
}

Actual behavior

The program throws an exception and the exception stack looks like MongoDB driver internal error

Duration 00:00:00.0091552
Unhandled exception. MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
 at MongoDB.Driver.Core.Connections.BinaryConnection.ReceiveMessage(Int32 responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.Connections.HelloHelper.GetResult(IConnection connection, CommandWireProtocol`1 helloProtocol, CancellationToken cancellationToken)
 at MongoDB.Driver.Core.Connections.ConnectionInitializer.SendHello(IConnection connection, CancellationToken cancellationToken)

(I clipped the rest of the stack)

Expected behavior

On top of the stack trace should have been a frame with my faulty code. Without that, it is hard to find if there is an error in my code.

My suggestion

I believe the stack trace is cut because of `throw ex` in this code https://github.com/mongodb/mongo-csharp-driver/blob/1123d60823c8c3fc42246282cddfecc8b76479e6/src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs#L522 (and some other places) If an exception is not wrapped, then it should be just 'throw', without ex.



 Comments   
Comment by Githook User [ 04/Jan/22 ]

Author:

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

Message: CSHARP-3990: Use `throw;` instead of `throw ex;` when re-throwing an unwrapped exception in BinaryConnection.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/95f8358d19c784e5f3e212c76b405a30d02a3c56

Comment by Robert Stam [ 09/Dec/21 ]

Thank you for reporting this.

I agree with your analysis that the issue is caused by calling `throw ex` instead of using `throw` to re-throw.

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