Skip to content

RUBY-3838 Fix raise_on_invalid handling and protect SRV monitor thread#3040

Open
comandeo-mongo wants to merge 1 commit intomongodb:masterfrom
comandeo-mongo:RUBY-3838-srv-resolver-raise-on-invalid
Open

RUBY-3838 Fix raise_on_invalid handling and protect SRV monitor thread#3040
comandeo-mongo wants to merge 1 commit intomongodb:masterfrom
comandeo-mongo:RUBY-3838-srv-resolver-raise-on-invalid

Conversation

@comandeo-mongo
Copy link
Copy Markdown
Contributor

@comandeo-mongo comandeo-mongo commented May 5, 2026

Background

Mongo::Srv::Resolver#raise_on_invalid? always returns true because of
operator precedence in @options[:raise_on_invalid] || true.

Changes

  • lib/mongo/srv/resolver.rbraise_on_invalid? now uses
    @options.fetch(:raise_on_invalid, true). The ||= memoization on a
    Boolean is also dropped (same footgun as the || precedence bug).
  • lib/mongo/srv/monitor.rb — the polling resolver is now constructed
    with raise_on_invalid: false per the polling-srv-records spec
    (mismatched-domain records and empty results MUST be logged and skipped,
    not raised). Adds a defensive rescue Mongo::Error in scan! so any
    Mongo error leaking out of the resolver cannot terminate the SRV monitor
    thread.
  • lib/mongo/uri/srv_protocol.rbRUBY-3743 changed this resolver to
    raise_on_invalid: false, but the resolver bug masked the change so it
    was never live. The Initial DNS Seedlist Discovery spec mandates that an
    error MUST be raised on domain mismatch or empty result during initial URI
    parsing, so this is reverted to the default raise_on_invalid: true and
    the docstring is corrected.

Spec citations

  • Polling SRV Records for mongos discovery: "MUST NOT raise an error" for
    both mismatched-domain records and empty/error DNS results (lines 60, 66
    of polling-srv-records-for-mongos-discovery.md).
  • Initial DNS Seedlist Discovery: "an error MUST be raised" for empty/no
    records, and "Drivers MUST raise an error and MUST NOT initiate a
    connection" for hostnames not matching the parent domain (lines 123-130).

Test plan

  • New unit specs in spec/mongo/srv/resolver_spec.rb cover both branches
    of raise_on_invalid for mismatched-domain and empty-result cases.
  • New monitor specs verify scan! does not propagate
    MismatchedDomain or NoSRVRecords from the resolver.
  • bundle exec rspec spec/mongo/srv/ spec/mongo/uri/srv_protocol_spec.rb
    → 218 examples, 0 failures.
  • bundle exec rspec spec/spec_tests/seed_list_discovery_spec.rb → 106
    examples, 0 failures.
  • bundle exec rubocop clean on touched files.

Jira

https://jira.mongodb.org/browse/RUBY-3838

Three related fixes around SRV resolution:

1. Resolver#raise_on_invalid? used `@options[:raise_on_invalid] || true`,
   which always returned true because of operator precedence. Replace with
   `@options.fetch(:raise_on_invalid, true)`.

2. Srv::Monitor#scan! did not rescue Mongo::Error, so a Mongo::Error::
   MismatchedDomain (or NoSRVRecords) raised by the resolver would tear down
   the SRV polling thread and freeze topology updates. Construct the polling
   resolver with raise_on_invalid: false (per the polling-srv-records spec)
   and add a defensive rescue Mongo::Error in scan!.

3. URI::SRVProtocol#resolver was switched to raise_on_invalid: false in
   RUBY-3743, but the bug in #1 masked the change so it was never live. The
   Initial DNS Seedlist Discovery spec mandates that an error MUST be raised
   on domain mismatch or empty result during initial URI parsing, so revert
   to the default raise_on_invalid: true and correct the docstring.
@comandeo-mongo comandeo-mongo marked this pull request as ready for review May 5, 2026 16:09
Copilot AI review requested due to automatic review settings May 5, 2026 16:09
@comandeo-mongo comandeo-mongo requested a review from a team as a code owner May 5, 2026 16:09
@comandeo-mongo comandeo-mongo requested a review from jamis May 5, 2026 16:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SRV resolution error-handling so raise_on_invalid behaves as intended, aligns initial vs polling SRV behavior with the relevant specs, and hardens the SRV monitor loop against unexpected resolver-raised Mongo::Errors that could otherwise terminate the monitor thread.

Changes:

  • Correct Mongo::Srv::Resolver#raise_on_invalid? to properly honor :raise_on_invalid with a default of true.
  • Force the SRV polling monitor’s resolver into “log-and-continue” mode (raise_on_invalid: false) and defensively rescue Mongo::Error in scan!.
  • Revert initial URI parsing resolver behavior to default “raise on invalid” and update the SRVProtocol resolver docstring accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
lib/mongo/srv/resolver.rb Fixes raise_on_invalid? defaulting logic so false is respected and default true works correctly.
lib/mongo/srv/monitor.rb Ensures polling SRV lookups don’t raise on invalid/empty results and prevents monitor thread termination from leaked Mongo::Errors.
lib/mongo/uri/srv_protocol.rb Restores initial seedlist discovery behavior to raise on invalid/empty SRV results and updates documentation to match spec intent.
spec/mongo/srv/resolver_spec.rb Adds unit coverage for both raise_on_invalid: false and default true behaviors for mismatched-domain and empty results.
spec/mongo/srv/monitor_spec.rb Adds coverage asserting scan! does not propagate MismatchedDomain / NoSRVRecords from the resolver and keeps topology unchanged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants