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!

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.

3 participants