From 2d6a8c6b6dc4cf3346730be39d7542b19573eca9 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <238531+indutny@users.noreply.github.com> Date: Thu, 1 Jun 2023 13:53:56 -0700 Subject: [PATCH] net: fix address iteration with autoSelectFamily MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `autoSelectFamily` is set to `true`, `net.connect` is supposed to try connecting to both IPv4 and IPv6, interleaving the address types. Instead, it appears that the array that holds the addresses in the order they should be attempted was never used after being populated. PR-URL: https://github.com/nodejs/node/pull/48258 Backport-PR-URL: https://github.com/nodejs/node/pull/48275 Reviewed-By: Paolo Insogna Reviewed-By: Colin Ihrig Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca Reviewed-By: Juan José Arboleda --- lib/net.js | 2 +- test/parallel/test-net-autoselectfamily.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index afcb9384a2fefa..a4893adb89c740 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1417,7 +1417,7 @@ function lookupAndConnectMultiple(self, async_id_symbol, lookup, host, options, const context = { socket: self, - addresses, + addresses: toAttempt, current: 0, port, localPort, diff --git a/test/parallel/test-net-autoselectfamily.js b/test/parallel/test-net-autoselectfamily.js index 43ae91f61c1879..2c1fef1c205865 100644 --- a/test/parallel/test-net-autoselectfamily.js +++ b/test/parallel/test-net-autoselectfamily.js @@ -117,7 +117,7 @@ function createDnsServer(ipv6Addrs, ipv4Addrs, cb) { // Test that only the last successful connection is established. { createDnsServer( - '::1', + ['2606:4700::6810:85e5', '2606:4700::6810:84e5', '::1'], ['104.20.22.46', '104.20.23.46', '127.0.0.1'], common.mustCall(function({ dnsServer, lookup }) { const ipv4Server = createServer((socket) => { @@ -144,7 +144,14 @@ function createDnsServer(ipv6Addrs, ipv4Addrs, cb) { connection.on('ready', common.mustCall(() => { assert.deepStrictEqual( connection.autoSelectFamilyAttemptedAddresses, - [`::1:${port}`, `104.20.22.46:${port}`, `104.20.23.46:${port}`, `127.0.0.1:${port}`] + [ + `2606:4700::6810:85e5:${port}`, + `104.20.22.46:${port}`, + `2606:4700::6810:84e5:${port}`, + `104.20.23.46:${port}`, + `::1:${port}`, + `127.0.0.1:${port}`, + ] ); }));