fix(NODE-7298): ensure commonWireVersion is computed from server maxWireVersion #4805
+137 −73
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.
Description
Summary of Changes
The TopologyDescription constructor initialized commonWireVersion to 0, but the update() method only updates this field when its previous value is null. As a result, commonWireVersion remained stuck at 0 even when all servers reported valid non-zero maxWireVersion values.
This caused secondaryWritableServerSelector() to treat the cluster as pre–MongoDB 5.0 (because wireVersion < 13) and always fall back to primary read preference. User-provided read preferences (e.g. secondary, secondaryPreferred) were therefore ignored for aggregation pipelines containing write stages ($merge and $out).
The fix initializes commonWireVersion to null, allowing it to be correctly updated to the minimum non-zero maxWireVersion across server descriptions.
A new test file
test/unit/sdam/topology_description.test.tshas been added to cover initialization and update behavior.Release Highlight
Fixed Read Preference adherence for
$mergeand$outaggregations.Resolved an issue where the driver failed to detect MongoDB 5.0+ capabilities due to incorrect
commonWireVersioninitialization. This ensures that aggregations with write stages now correctly respectsecondaryandsecondaryPreferredread preferences, rather than forcing execution on the Primary.Huge thanks to @crehbichler for discovering, investigating this bug and for implementing a fix!
Double check the following
npm run check:lint)type(NODE-xxxx)[!]: description