Skip to content

Commit

Permalink
net: fix connect crash when call destroy in lookup handler
Browse files Browse the repository at this point in the history
PR-URL: #51826
Fixes: #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]>
  • Loading branch information
theanarkh authored and marco-ippolito committed Feb 29, 2024
1 parent db30428 commit c6cc3ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-destroy-socket-in-lookup.js
Original file line number Diff line number Diff line change
@@ -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();
}));

0 comments on commit c6cc3ed

Please sign in to comment.