Skip to content

Fix the flaky udp port check test - #129

Merged
A21-devs merged 1 commit into
mainfrom
fix/flaky-port-test
Dec 2, 2026
Merged

A21-devs merged 1 commit into
mainfrom
fix/flaky-port-test

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Closes #78

sees a udp port that is bound bound UDP on a random port and then asserted the TCP side of the same number was free. The OS only promised the UDP side, and on Windows TCP and UDP ephemeral ports come out of the same range, so anything else on the machine holding that number on TCP failed the test. It hit my machine twice and CI once (#115).

  • The UDP test now only checks UDP: bound reads as taken, closed reads as free.
  • A new test, checks tcp and udp separately, binds UDP ports until it finds one whose TCP side reads free (up to 20 tries). It still fails if isPortFree ever confused the two protocols, since then no port would pass.

No change to app code.

Testing

  • npx vitest run tests/portCheck.test.ts (3 passed)
  • npm run typecheck, npm test (143 passed), npm run build

@A21-devs A21-devs added bug Something isn't working tests labels Dec 2, 2026
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.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the whole diff. Test-only change, nothing in app code moves.

  • The udp test no longer depends on anything outside its own socket.
  • The new test still catches a mixed-up protocol in isPortFree: if it treated a udp bind as a tcp bind, all 20 ports would read taken and it throws.
  • return inside the try still runs the finally, so the socket is closed on the success path too.

One thing I looked at and left: the tcp test's last line (closed port reads free) has the same kind of gap in theory, since another process could take the number between close and the check. That window is a few ms right after we held the port ourselves, unlike the old udp case where the other side was never ours. Not worth a loop. Looks good.

@A21-devs
A21-devs merged commit d3ffef1 into main Dec 2, 2026
1 check passed
@A21-devs
A21-devs deleted the fix/flaky-port-test branch December 2, 2026 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: portCheck assumes the TCP side of a random UDP port is free

1 participant