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

Disposing a cursor that used a connection that failed leads to NullReferenceException

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 1.1
    • None
    • None
    • None

    Description

      The stack trace is as follows:
      System.NullReferenceException: Object reference not set to an instance of an object.
      at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, MongoServerInstance serverInstance) in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoServer.cs:line 827
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.AcquireConnection() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 142
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.KillCursor() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 222
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.Dispose() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 69
      at mongotest.Program.ReadTest() in tools\mongotest\mongotest\Program.cs:line 122

      The MongoConnection.HandleException() terminates server instance, destroying it's connection pool, but when cursor is disposed, it tries to acquire connection to properly kill itself.
      I guess that MongoCursorEnumerator.Dispose() or MongoCursorEnumerator.KillCursor() should either not acquire a connection from a dead server (TryAcquireConnection or something like that) or blindly ignore exceptions when connection is acquired (while this way is easier, I don't think that ignoring NREs is a good idea after all).

      Also, adding some kind of a null check to MongoServerInstance.ConnectionPool and throwing ObjectDisposedException (or InvalidOperationException, since MongoServerInstance isn't actually an IDisposable, it's all up to you) would be nice, because NREs are hard to debug in optimized code because of method inlining even online, while postmortem dump-based debugging often becomes really nasty =)

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            onyxmaster Aristarkh Zagorodnikov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: