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

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

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.1
    • Affects Version/s: None
    • Component/s: None
    • None

      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 =)

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

              Created:
              Updated:
              Resolved: