Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/runtime/dns_jsc/cares_jsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,21 @@ fn any_reply_to_js(
}

// ── Error ──────────────────────────────────────────────────────────────────

/// Node's `dnsException` only assigns a numeric `errno` when libuv reported a
/// numeric error (the getaddrinfo/getnameinfo family). c-ares resolver
/// failures carry a string code, so Node leaves their `errno` undefined;
/// overwrite the c-ares enum value `SystemError` put there.
Comment thread
robobun marked this conversation as resolved.
Outdated
fn clear_errno_for_resolver_syscall(
instance: JSValue,
global_this: &JSGlobalObject,
syscall: &[u8],
) {
if strings::has_prefix_comptime(syscall, b"query") || strings::eql(syscall, b"getHostByAddr") {
instance.put(global_this, b"errno", JSValue::UNDEFINED);
}
}

pub(crate) struct ErrorDeferred {
pub errno: c_ares::Error,
pub syscall: &'static [u8],
Expand Down Expand Up @@ -694,6 +709,7 @@ impl ErrorDeferred {
b"name",
bstr::String::static_(b"DNSException").to_js(global_this)?,
);
clear_errno_for_resolver_syscall(instance, global_this, self.syscall);

// `self` (and thus self.promise / self.hostname) drops at scope exit;
// hostname was `take()`n above to avoid double-deref.
Expand Down Expand Up @@ -782,6 +798,7 @@ pub(crate) fn error_to_js_with_syscall(
b"name",
bstr::String::static_(b"DNSException").to_js(global_this)?,
);
clear_errno_for_resolver_syscall(instance, global_this, syscall);
Ok(instance)
}

Expand Down Expand Up @@ -825,6 +842,7 @@ pub(crate) fn error_to_js_with_syscall_and_hostname(
b"name",
bstr::String::static_(b"DNSException").to_js(global_this)?,
);
clear_errno_for_resolver_syscall(instance, global_this, syscall);
Ok(instance)
}

Expand Down
36 changes: 36 additions & 0 deletions test/js/node/dns/node-dns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ test.skipIf(isWindows)("dns.resolveSrv accepts compressed target in RDATA", asyn
}
});

// Node only sets a numeric `errno` on DNS errors when libuv reported a
// numeric error (getaddrinfo/getnameinfo). c-ares resolver failures carry a
// string code and leave `errno` undefined. https://github.com/oven-sh/bun/issues/37320
test.skipIf(isWindows)("resolver query errors leave errno undefined", async () => {
const socket = dgram.createSocket("udp4");
try {
socket.on("message", (query, rinfo) => {
// Reply NXDOMAIN to every query.
const res = Buffer.from(query);
res[2] = 0x81; // QR=1, RD=1
res[3] = 0x83; // RA=1, RCODE=3 (NXDOMAIN)
res.fill(0, 6, 12); // ANCOUNT/NSCOUNT/ARCOUNT = 0
socket.send(res, rinfo.port, rinfo.address);
});
socket.bind(0, "127.0.0.1");
await once(socket, "listening");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const { port } = socket.address();

const resolver = new dns.Resolver({ timeout: 1000, tries: 1 });
resolver.setServers(["127.0.0.1:" + port]);

const queryErr = await new Promise(resolve => resolver.resolve4("invalid.invalid", err => resolve(err)));
expect(queryErr.code).toBe("ENOTFOUND");
expect(queryErr.syscall).toBe("queryA");
expect(queryErr.hostname).toBe("invalid.invalid");
expect(queryErr.errno).toBeUndefined();

const reverseErr = await new Promise(resolve => resolver.reverse("192.0.2.1", err => resolve(err)));
expect(reverseErr.code).toBe("ENOTFOUND");
expect(reverseErr.syscall).toBe("getHostByAddr");
expect(reverseErr.errno).toBeUndefined();
Comment thread
robobun marked this conversation as resolved.
Outdated
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
} finally {
socket.close();
}
});

test("dns.resolveTxt (txt.socketify.dev)", () => {
const { promise, resolve, reject } = Promise.withResolvers();
dns.resolveTxt("txt.socketify.dev", (err, results) => {
Expand Down