net: fix connect crash when call destroy in lookup handler

PR-URL: https://github.com/nodejs/node/pull/51826
Fixes: https://github.com/nodejs/node/issues/50841
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
This commit is contained in:
theanarkh
2024-02-27 21:46:45 +08:00
committed by GitHub
parent 7824a79eba
commit 292016c91e
2 changed files with 20 additions and 1 deletions

View File

@@ -1435,7 +1435,10 @@ function lookupAndConnectMultiple(
const address = addresses[i];
const { address: ip, family: addressType } = address;
self.emit('lookup', err, ip, addressType, host);
// It's possible we were destroyed while looking this up.
if (!self.connecting) {
return;
}
if (isIP(ip) && (addressType === 4 || addressType === 6)) {
if (!destinations) {
destinations = addressType === 6 ? { 6: 0, 4: 1 } : { 4: 0, 6: 1 };

View File

@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const net = require('net');
// Test that the process does not crash.
const socket = net.connect({
port: 12345,
host: 'localhost',
// Make sure autoSelectFamily is true
// so that lookupAndConnectMultiple is called.
autoSelectFamily: true,
});
// DNS resolution fails or succeeds
socket.on('lookup', common.mustCall(() => {
socket.destroy();
}));