-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: do not attempt new connections when aborted
- Loading branch information
1 parent
d4bcdd8
commit e372bf7
Showing
2 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
test/parallel/test-net-autoselectfamily-timeout-cancelling.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
Check failure on line 1 in test/parallel/test-net-autoselectfamily-timeout-cancelling.js GitHub Actions / test-macOS
Check failure on line 1 in test/parallel/test-net-autoselectfamily-timeout-cancelling.js GitHub Actions / test-asan
Check failure on line 1 in test/parallel/test-net-autoselectfamily-timeout-cancelling.js GitHub Actions / test-linux
|
||
|
||
const common = require('../common'); | ||
const { addresses: { INET4_IP } } = require('../common/internet'); | ||
const { parseDNSPacket, writeDNSPacket } = require('../common/dns'); | ||
|
||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
const { Resolver } = require('dns'); | ||
const { createConnection } = require('net'); | ||
|
||
// Test that happy eyeballs algorithm properly handles cancellations. | ||
|
||
// Purposely set this to a low value because we want all connection but the last to fail | ||
const autoSelectFamilyAttemptTimeout = 15; | ||
|
||
function _lookup(resolver, hostname, options, cb) { | ||
resolver.resolve(hostname, 'ANY', (err, replies) => { | ||
assert.notStrictEqual(options.family, 4); | ||
|
||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
const hosts = replies | ||
.map((r) => ({ address: r.address, family: r.type === 'AAAA' ? 6 : 4 })) | ||
.sort((a, b) => b.family - a.family); | ||
|
||
if (options.all === true) { | ||
return cb(null, hosts); | ||
} | ||
|
||
return cb(null, hosts[0].address, hosts[0].family); | ||
}); | ||
} | ||
|
||
function createDnsServer(ipv6Addrs, ipv4Addrs, cb) { | ||
if (!Array.isArray(ipv6Addrs)) { | ||
ipv6Addrs = [ipv6Addrs]; | ||
} | ||
|
||
if (!Array.isArray(ipv4Addrs)) { | ||
ipv4Addrs = [ipv4Addrs]; | ||
} | ||
|
||
// Create a DNS server which replies with a AAAA and a A record for the same host | ||
const socket = dgram.createSocket('udp4'); | ||
|
||
socket.on('message', common.mustCall((msg, { address, port }) => { | ||
const parsed = parseDNSPacket(msg); | ||
const domain = parsed.questions[0].domain; | ||
assert.strictEqual(domain, 'example.org'); | ||
|
||
socket.send(writeDNSPacket({ | ||
id: parsed.id, | ||
questions: parsed.questions, | ||
answers: [ | ||
...ipv6Addrs.map((address) => ({ type: 'AAAA', address, ttl: 123, domain: 'example.org' })), | ||
...ipv4Addrs.map((address) => ({ type: 'A', address, ttl: 123, domain: 'example.org' })), | ||
] | ||
}), port, address); | ||
})); | ||
|
||
socket.bind(0, () => { | ||
const resolver = new Resolver(); | ||
resolver.setServers([`127.0.0.1:${socket.address().port}`]); | ||
|
||
cb({ dnsServer: socket, lookup: _lookup.bind(null, resolver) }); | ||
}); | ||
} | ||
|
||
// Test that if a connection attempt is aborted before finishing the connection, then the process does not crash | ||
{ | ||
createDnsServer([], [INET4_IP, '127.0.0.1', INET4_IP], common.mustCall(function({ dnsServer, lookup }) { | ||
const connection = createConnection({ | ||
host: 'example.org', | ||
port: 443, | ||
lookup, | ||
autoSelectFamily: true, | ||
autoSelectFamilyAttemptTimeout, | ||
}); | ||
|
||
let destroyTimeoutSet = false; | ||
connection.on('lookup', common.mustCall(() => { | ||
if (destroyTimeoutSet) { | ||
return; | ||
} | ||
|
||
destroyTimeoutSet = true; | ||
setTimeout(() => { | ||
connection.destroy(); | ||
}, autoSelectFamilyAttemptTimeout); | ||
}, 3)); | ||
|
||
connection.on('ready', common.mustNotCall()); | ||
connection.on('close', common.mustCall(() => { | ||
dnsServer.close(); | ||
})); | ||
})); | ||
} |