Skip to content

pawel-guzek/mongo-srv-issue

Repository files navigation

SRV seed list is not refreshed after DNS error when using mongodb+srv with replicaSet parameter

Description

When using a mongodb+srv connection string with the replicaSet parameter, the driver stops monitoring the SRV record after a DNS error and does not attempt to refresh the seed list again.

Steps to Reproduce

  1. Use a connection string with replicaSet parameter, for example:
    mongodb+srv://user:[email protected]/db1?replicaSet=db1-rs0
    
  2. Simulate a DNS resolution failure for the SRV record (eg. by providing invalid domain or overriding DnsResolver).
  3. Observe that after the first DNS error, the driver does not retry SRV lookups. For comparison, repeat the test without the replicaSet parameter and note that the driver retries SRV lookups every 10 seconds as expected.

Expected Behavior

  1. Monitoring SRV record until DNS is available

    The driver could continue retrying SRV lookups at the regular interval after a DNS error, regardless of whether replicaSet is specified. SRV monitoring should only stop after a successful resolution that yields a valid seed list.

  2. Fail creation of MongoClient

    The creation of MongoClient instance could fail when DNS error occurs to avoid creation of stale instance

Actual Behavior

When a replicaSet parameter is specified, SRV monitoring stops after the first DNS error and does not resume. This leaves the driver with a stale or empty seed list, preventing discovery of replica set members.

Technical Details / Root Cause

This behavior appears related to:

  • com.mongodb.internal.connection.DefaultDnsSrvRecordMonitor.DnsSrvRecordMonitorRunnable
    • clusterType = dnsSrvRecordInitializer.getClusterType() call
    • shouldContinueMonitoring method
  • clusterType value derived from ClusterSettings#getRequiredClusterType

When a replicaSet parameter is present, ClusterSettings#getRequiredClusterType sets requiredClusterType to REPLICA_SET, which appears to cause shouldContinueMonitoring to return false after a DNS error

if (builder.requiredReplicaSetName != null) {
    if (builder.requiredClusterType == ClusterType.UNKNOWN) {
        builder.requiredClusterType = ClusterType.REPLICA_SET;
    } else if (builder.requiredClusterType != ClusterType.REPLICA_SET) {
        throw new IllegalArgumentException(...);
    }
}

As a result, the DnsSrvRecordMonitorRunnable thread terminates and no further SRV refreshes are attempted.

Environment

Java driver version: 5.5.1

Java version: 21

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages