Skip to content

Conversation

@NotVivek12
Copy link

dns: fix Windows SRV ECONNREFUSED regression by correcting c-ares fallback detection

Summary

This PR fixes a regression introduced in Node.js v24.13.0 on Windows where DNS SRV lookups (commonly used by MongoDB connection strings) fail with ECONNREFUSED.

The issue stems from a change in c-ares behavior where the fallback resolver (loopback) is reported with port 53 rather than port 0. The existing Node.js glue layer strictly expected port 0 to identify a fallback scenario. Consequently, Node.js failed to detect the fallback, attempting to query a non-listening local stub resolver at 127.0.0.1:53 or [::1]:53.

Changes

  1. **Updated ChannelWrap::EnsureServers**: Modified logic to treat any single loopback server (IPv4 127.0.0.1 or IPv6 ::1) as a fallback configuration, regardless of the port number reported by c-ares.
  2. Proactive Validation: Added a call to EnsureServers() immediately before dispatching queries in the Query template to ensure the channel is correctly initialized before use.

Regression Details

  • Affected Version: Node.js v24.13.0
  • Platform: Windows 11 / Server
  • Error: Error: querySrv ECONNREFUSED
  • Last Known Good: v24.11.1

Reproduction

This issue is 100% reproducible on Windows with Node v24.13.0 using the following script (mimicking a standard Mongoose connection):

const express = require("express");
const mongoose = require("mongoose");
const app = express();

const connectDB = async () => {
  try {
    // Replace with a valid SRV URI
    const conn = await mongoose.connect("mongodb+srv://<user>:<pass>@cluster0.example.mongodb.net/db");
    console.log(`MongoDB Connected: ${conn.connection.host}`);
  } catch (error) {
    console.error('MongoDB connection failed:', error.message);
    process.exit(1);
  }
};
connectDB();

app.listen(3300, () => {
  console.log("mongodb connected at port 3300");
});

Output on v24.13.0:
Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.example.mongodb.net

Manual Verification

  1. Built Node.js from source on Windows (VS 2022 Build Tools).
  2. Ran the reproduction script above; connection succeeds.
  3. Verified dns.getServers() no longer returns a single loopback address when the system resolver is active.

Risks

Low.

  • The logic only activates when c-ares discovers a single loopback server and the user has not manually called setServers().
  • If setServers() is called, is_servers_default_ becomes false, skipping this check entirely.
  • The logic is platform-agnostic but primarily resolves the specific behavior observed on Windows.
Workaround for users Users on v24.13.0 can temporarily work around this by forcing DNS servers before connection:
require('dns').setServers(['8.8.8.8', '1.1.1.1']);

Checklist

  • Fixes regression in dns / c-ares
  • Tested on Windows
  • Tests added/verified (Manual verification performed)

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. cares Issues and PRs related to the c-ares dependency or the cares_wrap binding. needs-ci PRs that need a full CI run. labels Jan 20, 2026
@Ethan-Arrowood
Copy link
Contributor

Do you think you could add a tests so we don't see this regression again in the future? Thanks!

@NotVivek12
Copy link
Author

Do you think you could add a tests so we don't see this regression again in the future? Thanks!

Absolutely, happy to add a test. Did you have a specific scenario in mind, or should I just cover the case from the original bug report?

@NotVivek12
Copy link
Author

Sure, I've added regression tests for this! Created two test files:

test-dns-resolvesrv.js- basic SRV resolution tests
test-dns-resolvesrv-econnrefused.js- specifically tests the ECONNREFUSED regression with MongoDB Atlas-style SRV lookups
Also added SRV record support to dns.js so we can mock SRV responses.

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Thank you @NotVivek12! This largely looks good, just have two questions here

TEST(HelloWorldTest, BasicAssertions) {
EXPECT_EQ(1 + 1, 2);
EXPECT_TRUE(true);
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

^ This file was included unintentionally, right?

static const unsigned char kIPv6Loopback[16] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
is_loopback =
(memcmp(&servers[0].addr.addr6, kIPv6Loopback, sizeof(kIPv6Loopback)) == 0);
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check for host/network byte order conversion here like we do for IPv4 (via htonl)?

@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

❌ Patch coverage is 11.11111% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.79%. Comparing base (83893bb) to head (2a528e2).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
src/cares_wrap.cc 11.11% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61453      +/-   ##
==========================================
+ Coverage   89.78%   89.79%   +0.01%     
==========================================
  Files         672      672              
  Lines      203809   203814       +5     
  Branches    39183    39179       -4     
==========================================
+ Hits       182980   183020      +40     
+ Misses      13166    13113      -53     
- Partials     7663     7681      +18     
Files with missing lines Coverage Δ
src/cares_wrap.cc 61.73% <11.11%> (+1.42%) ⬆️

... and 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. cares Issues and PRs related to the c-ares dependency or the cares_wrap binding. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants