Skip to content

Commit c6cc3ed

Browse files
theanarkhmarco-ippolito
authored andcommitted
net: fix connect crash when call destroy in lookup handler
PR-URL: #51826Fixes: #50841 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent db30428 commit c6cc3ed

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

‎lib/net.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,10 @@ function lookupAndConnectMultiple(
14351435
constaddress=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};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constnet=require('net');
4+
5+
// Test that the process does not crash.
6+
constsocket=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)