Skip to content

Commit c6cc3ed

Browse files
theanarkhmarco-ippolito
authored andcommitted
net: fix connect crash when call destroy in lookup handler
PR-URL: #51826 Fixes: #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>
1 parent db30428 commit c6cc3ed

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/net.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,10 @@ function lookupAndConnectMultiple(
14351435
const address = addresses[i];
14361436
const { address: ip, family: addressType } = address;
14371437
self.emit('lookup', err, ip, addressType, host);
1438-
1438+
// It's possible we were destroyed while looking this up.
1439+
if (!self.connecting) {
1440+
return;
1441+
}
14391442
if (isIP(ip) && (addressType === 4 || addressType === 6)) {
14401443
if (!destinations) {
14411444
destinations = addressType === 6 ? { 6: 0, 4: 1 } : { 4: 0, 6: 1 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const net = require('net');
4+
5+
// Test that the process does not crash.
6+
const socket = net.connect({
7+
port: 12345,
8+
host: 'localhost',
9+
// Make sure autoSelectFamily is true
10+
// so that lookupAndConnectMultiple is called.
11+
autoSelectFamily: true,
12+
});
13+
// DNS resolution fails or succeeds
14+
socket.on('lookup', common.mustCall(() => {
15+
socket.destroy();
16+
}));

0 commit comments

Comments
 (0)