RUBY-3838 Fix raise_on_invalid handling and protect SRV monitor thread#3040
Open
comandeo-mongo wants to merge 1 commit intomongodb:masterfrom
Open
RUBY-3838 Fix raise_on_invalid handling and protect SRV monitor thread#3040comandeo-mongo wants to merge 1 commit intomongodb:masterfrom
comandeo-mongo wants to merge 1 commit intomongodb:masterfrom
Conversation
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.
Contributor
There was a problem hiding this comment.
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_invalidwith a default oftrue. - Force the SRV polling monitor’s resolver into “log-and-continue” mode (
raise_on_invalid: false) and defensively rescueMongo::Errorinscan!. - 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.
jamis
approved these changes
May 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
Mongo::Srv::Resolver#raise_on_invalid?always returnstruebecause ofoperator precedence in
@options[:raise_on_invalid] || true.Changes
lib/mongo/srv/resolver.rb—raise_on_invalid?now uses@options.fetch(:raise_on_invalid, true). The||=memoization on aBoolean is also dropped (same footgun as the
||precedence bug).lib/mongo/srv/monitor.rb— the polling resolver is now constructedwith
raise_on_invalid: falseper the polling-srv-records spec(mismatched-domain records and empty results MUST be logged and skipped,
not raised). Adds a defensive
rescue Mongo::Errorinscan!so anyMongo error leaking out of the resolver cannot terminate the SRV monitor
thread.
lib/mongo/uri/srv_protocol.rb— RUBY-3743 changed this resolver toraise_on_invalid: false, but the resolver bug masked the change so itwas 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: trueandthe docstring is corrected.
Spec citations
both mismatched-domain records and empty/error DNS results (lines 60, 66
of
polling-srv-records-for-mongos-discovery.md).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
spec/mongo/srv/resolver_spec.rbcover both branchesof
raise_on_invalidfor mismatched-domain and empty-result cases.scan!does not propagateMismatchedDomainorNoSRVRecordsfrom 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→ 106examples, 0 failures.
bundle exec rubocopclean on touched files.Jira
https://jira.mongodb.org/browse/RUBY-3838