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

Strange NullReferenceException on acquiring connection

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.1
    • Affects Version/s: 1.1
    • Component/s: None
    • None
    • Environment:
      I'm using a version built from source (git commit 1091cad...).

      Today, I got a strange exception:
      System.NullReferenceException: Object reference not set to an instance of an object.
      at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, MongoServerInstance serverInstance) in mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoServer.cs:line 826
      at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, Boolean slaveOk) in mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoServer.cs:line 819
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.AcquireConnection() in mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 137
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.GetFirst() in mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 147
      at MongoDB.Driver.Internal.MongoCursorEnumerator`1.MoveNext() in mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 83
      at MongoDB.Driver.MongoDatabase.GetCollectionNames() in mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoDatabase.cs:line 560
      at MongoDB.Driver.MongoDatabase.CollectionExists(String collectionName) in mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoDatabase.cs:line 277

      My code:
      var connectionString = "mongodb://server1,server2/databasename?replicaSet=replicaSetName"
      var urlBuilder = new MongoUrlBuilder(connectionString);
      urlBuilder.SafeMode = SafeMode.True;
      var db = MongoDatabase.Create(urlBuilder.ToMongoUrl());
      db.CollectionExists("collectionName"); // Exception was thrown here

      The replica set consists of three machines. At the time of failure their roles were as follows:
      server1: primary
      server2: secondary
      server3: arbiter (it was not specified in the connection string)

      The exception only appeared once (my code is called from a lazy initialization code that is from a web page, so when I refreshed the page, the code that failed got called another one time and this time it was OK). This is a first operation on the database, slaveOk is set to false, so it appears that there's a problem in ReplicaSetConnector.Connect() so that it returns before it's able to acquire connection to primary, or instance list or metadata becomes damaged so that MongoServer.Primary returns null even if there is an instance, to me this looks like a race condition.

            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: