From de58b77f1ffcdc7646c5bf341e66f917bfb0f465 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:57:01 -0500 Subject: [PATCH 1/3] Port check tests: handle socket and server errors A bind or listen that failed emitted 'error' with no handler, so vitest reported it against whichever test happened to be running. Each socket and server the tests create now rejects with its own error. --- tests/portCheck.test.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/portCheck.test.ts b/tests/portCheck.test.ts index 9075eca..429b9a2 100644 --- a/tests/portCheck.test.ts +++ b/tests/portCheck.test.ts @@ -1,12 +1,29 @@ -import { createSocket } from 'node:dgram' +import { createSocket, type Socket } from 'node:dgram' import { createServer, type AddressInfo } from 'node:net' import { describe, expect, it } from 'vitest' import { isPortFree } from '../src/main/vmRunner' +/** Binds a udp socket, rejecting with the bind's own error instead of leaving it unhandled. */ +function bindUdp(port: number): Promise { + return new Promise((resolve, reject) => { + const sock = createSocket('udp4') + sock.once('error', (err) => { + sock.close() + reject(err) + }) + sock.bind(port, '127.0.0.1', () => resolve(sock)) + }) +} + +const closeUdp = (sock: Socket): Promise => new Promise((r) => sock.close(() => r())) + describe('isPortFree', () => { it('sees a tcp port that is being listened on', async () => { const srv = createServer() - await new Promise((r) => srv.listen(0, '127.0.0.1', () => r())) + await new Promise((resolve, reject) => { + srv.once('error', reject) + srv.listen(0, '127.0.0.1', () => resolve()) + }) const port = (srv.address() as AddressInfo).port try { expect(await isPortFree(port, 'tcp')).toBe(false) @@ -17,13 +34,12 @@ describe('isPortFree', () => { }) it('sees a udp port that is bound', async () => { - const sock = createSocket('udp4') - await new Promise((r) => sock.bind(0, '127.0.0.1', () => r())) + const sock = await bindUdp(0) const port = sock.address().port try { expect(await isPortFree(port, 'udp')).toBe(false) } finally { - await new Promise((r) => sock.close(() => r())) + await closeUdp(sock) } expect(await isPortFree(port, 'udp')).toBe(true) }) @@ -32,13 +48,12 @@ describe('isPortFree', () => { // The OS only promises the udp port it hands out is free on udp. Another // program can hold the same number on tcp, so look for one where it doesn't. for (let attempt = 0; attempt < 20; attempt++) { - const sock = createSocket('udp4') - await new Promise((r) => sock.bind(0, '127.0.0.1', () => r())) + const sock = await bindUdp(0) const port = sock.address().port try { if (await isPortFree(port, 'tcp')) return } finally { - await new Promise((r) => sock.close(() => r())) + await closeUdp(sock) } } throw new Error('every bound udp port also read as taken on tcp') From 1adf76727753fe6c97ec331dfa6584f880837f0f Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:57:02 -0500 Subject: [PATCH 2/3] Port check test: start from a port free on tcp The test bound udp to port 0 and looked for one that was also free on tcp. Windows hands out udp ports one after another, and Hyper-V reserves tcp ports in blocks of 100 (netsh int ipv4 show excludedportrange protocol=tcp). When the counter is inside such a block, all 20 attempts read as taken on tcp and the test throws within a few milliseconds. Ask for a free tcp port first and hold that number on udp instead. A tcp port the OS hands out is never in an excluded range. The test now also checks the udp side reads as taken while the socket holds it. --- tests/portCheck.test.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/portCheck.test.ts b/tests/portCheck.test.ts index 429b9a2..8271fce 100644 --- a/tests/portCheck.test.ts +++ b/tests/portCheck.test.ts @@ -15,6 +15,18 @@ function bindUdp(port: number): Promise { }) } +/** A port the OS says is free on tcp right now: listen on 0, read it back, close. */ +function freeTcpPort(): Promise { + return new Promise((resolve, reject) => { + const srv = createServer() + srv.once('error', reject) + srv.listen(0, '127.0.0.1', () => { + const port = (srv.address() as AddressInfo).port + srv.close(() => resolve(port)) + }) + }) +} + const closeUdp = (sock: Socket): Promise => new Promise((r) => sock.close(() => r())) describe('isPortFree', () => { @@ -45,17 +57,22 @@ describe('isPortFree', () => { }) it('checks tcp and udp separately', async () => { - // The OS only promises the udp port it hands out is free on udp. Another - // program can hold the same number on tcp, so look for one where it doesn't. + // Start from a port that is free on tcp and hold it on udp. Going the other + // way (bind udp to 0, hope it is free on tcp) fails in runs: Windows hands + // out udp ports in order, and Hyper-V reserves blocks of 100 tcp ports, so + // every attempt can land inside one reserved block. for (let attempt = 0; attempt < 20; attempt++) { - const sock = await bindUdp(0) - const port = sock.address().port + const port = await freeTcpPort() + const sock = await bindUdp(port).catch(() => null) // taken on udp, try another + if (!sock) continue try { + expect(await isPortFree(port, 'udp')).toBe(false) + // a tcp listener can still take it in between, so only a free reading counts if (await isPortFree(port, 'tcp')) return } finally { await closeUdp(sock) } } - throw new Error('every bound udp port also read as taken on tcp') + throw new Error('no port could be held on udp while free on tcp') }) }) From fe0508792c70d4738ed7940a4ab9bfff4e0fedf1 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:58:27 -0500 Subject: [PATCH 3/3] Port check test: drop the bind error handler once the bind is done Left on, it swallowed any later error on the socket instead of letting it fail the test. --- tests/portCheck.test.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/portCheck.test.ts b/tests/portCheck.test.ts index 8271fce..112db47 100644 --- a/tests/portCheck.test.ts +++ b/tests/portCheck.test.ts @@ -7,11 +7,16 @@ import { isPortFree } from '../src/main/vmRunner' function bindUdp(port: number): Promise { return new Promise((resolve, reject) => { const sock = createSocket('udp4') - sock.once('error', (err) => { + const onError = (err: Error): void => { sock.close() reject(err) + } + sock.once('error', onError) + sock.bind(port, '127.0.0.1', () => { + // only the bind's own error is ours to handle, later ones should fail the test + sock.off('error', onError) + resolve(sock) }) - sock.bind(port, '127.0.0.1', () => resolve(sock)) }) }