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

InvalidOperationException due to null casting

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.1.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Not Needed

      Summary

      An InvalidOperationException occurs due to a null casting issue in MongoDB C# driver when handling heartbeat events. This leads to a TimeoutException and client disconnection from the server.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      • Driver Version: 2.25.0

      How to Reproduce

      1. Configure MongoDB client settings with heartbeat event subscriptions as shown in the code example below.
      2. Run the code and wait for the heartbeat events to be triggered.
      var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017");
      
      mongoClientSettings.ClusterConfigurator += clusterBuilder =>
      {
          clusterBuilder.Subscribe<ServerHeartbeatStartedEvent>(
              ev => Console.WriteLine($"Server {0} heartbeat started (connId: { ev.ConnectionId.ServerValue})")
          );
      
          clusterBuilder.Subscribe<ServerHeartbeatSucceededEvent>(
              ev => Console.WriteLine($"Server {0} heartbeat succeeded (connId: { ev.ConnectionId.ServerValue})")
          );
      
          clusterBuilder.Subscribe<ServerHeartbeatFailedEvent>(
              ev => Console.WriteLine($"Server {0} heartbeat failed (connId: { ev.ConnectionId.ServerValue})")
          );
      };
      
      var mongoClient = new MongoClient(mongoClientSettings);
      
      var db = mongoClient.GetDatabase(Guid.NewGuid().ToString().Substring(0, 8));
      
      await db.CreateCollectionAsync("test");
      

      Exception

      System.TimeoutException : A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors = WritableServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }, OperationsCountServerSelector }. Client view of cluster state is { ClusterId : "2", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 2, EndPoint : "Unspecified/localhost:27017" }", EndPoint: "Unspecified/localhost:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "System.InvalidOperationException: Nullable object must have a value.
         at MongoDB.Driver.Core.Connections.ConnectionId.get_ServerValue()
      

      Additional Background

      The exception is triggered by an attempt to access ConnectionId.ServerValue, where ServerValue is null. The issue appears to be caused by changes introduced in CSHARP-4417: ConnectionId should use longs for LocalValue and ServerValue to match 64-bit integer connectionId returned from the server

      namespace MongoDB.Driver.Core.Connections
      {
          public sealed class ConnectionId : IEquatable<ConnectionId>
          {
              private readonly long? _serverValue;
      
              [Obsolete("Use LongServerValue instead.")]
              public int? ServerValue
              {
                  get { return (int)_serverValue; }
              }
          }
      }
      

            Assignee:
            boris.dogadov@mongodb.com Boris Dogadov
            Reporter:
            flibustier7seas@gmail.com flibustier seas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: