Skip to content

Commit 8d42625

Browse files
panvanodejs-github-bot
authored andcommitted
test: reserve both families for local port test
Reserve the source port with a dual-stack listener and keep it open until the destination server is listening. This prevents choosing an occupied IPv6 port or letting the destination reuse the source port. Refs: nodejs/reliability#1686 Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66722 Refs: nodejs/reliability#1687 Refs: nodejs/reliability#1690 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
1 parent 2745698 commit 8d42625

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎test/parallel/test-net-autoselectfamily-local-port.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ TCP.prototype.connect6 = function() {
1313
};
1414

1515
const reserved = createServer();
16-
reserved.listen(0, '127.0.0.1', common.mustCall(() => {
16+
// Reserve both address families: an IPv4-only reservation may choose a port
17+
// that is already in use by an IPv6-only socket.
18+
reserved.listen(0, '::', common.mustCall(() => {
1719
const localPort = reserved.address().port;
18-
reserved.close(common.mustCall(() => {
19-
const server = createServer(common.mustCall((socket) => socket.end()));
20-
server.listen(0, '127.0.0.1', common.mustCall(() => {
21-
const port = server.address().port;
20+
const server = createServer(common.mustCall((socket) => socket.end()));
21+
// Keep the reservation until the destination listens so it cannot take
22+
// the source port that the client is about to use.
23+
server.listen(0, '127.0.0.1', common.mustCall(() => {
24+
const port = server.address().port;
25+
assert.notStrictEqual(port, localPort);
26+
reserved.close(common.mustCall(() => {
2227
const connection = createConnection({
2328
host: 'example.org',
2429
port,

0 commit comments

Comments
 (0)