Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3990

Use `throw;` instead of `throw ex;` when re-throwing an unwrapped exception in BinaryConnection

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.15.0
    • Affects Version/s: 2.14.0
    • Component/s: None
    • None
    • Minor Change

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tolyandr@gmail.com Анатолий Буранов
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: