From 0c5c7181d73f15fb9fe170566891d0ceb0f0c71c Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Wed, 2 Dec 2026 18:42:37 -0500 Subject: [PATCH] portCheck test: don't assume the tcp side of a random udp port is free The OS only promises the udp port it hands out is free on udp. Another program can have the same number bound on tcp, which made the test fail now and then, locally and on CI. The udp test now only checks udp, and a separate test looks for a port whose tcp side is free, retrying a few times, to show the two are checked separately. --- tests/portCheck.test.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/portCheck.test.ts b/tests/portCheck.test.ts index 87a786d..9075eca 100644 --- a/tests/portCheck.test.ts +++ b/tests/portCheck.test.ts @@ -22,11 +22,25 @@ describe('isPortFree', () => { const port = sock.address().port try { expect(await isPortFree(port, 'udp')).toBe(false) - // tcp and udp ports are separate, the tcp side of it is still free - expect(await isPortFree(port, 'tcp')).toBe(true) } finally { await new Promise((r) => sock.close(() => r())) } expect(await isPortFree(port, 'udp')).toBe(true) }) + + 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. + for (let attempt = 0; attempt < 20; attempt++) { + const sock = createSocket('udp4') + await new Promise((r) => sock.bind(0, '127.0.0.1', () => r())) + const port = sock.address().port + try { + if (await isPortFree(port, 'tcp')) return + } finally { + await new Promise((r) => sock.close(() => r())) + } + } + throw new Error('every bound udp port also read as taken on tcp') + }) })