Skip to content

Port check test: stop landing in Hyper-V's reserved tcp ports - #161

Merged
A21-devs merged 3 commits into
mainfrom
tests/port-check-flake
Dec 5, 2026
Merged

A21-devs merged 3 commits into
mainfrom
tests/port-check-flake

Conversation

@A21-devs

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

Copy link
Copy Markdown
Owner

Closes #154

The guess in the issue (an unhandled bind error) turned out not to be the cause. What happens:

  • Windows hands out udp ports one after another. Binding to port 0 twenty times in a row here gave 50561, 50562, ... 50580, and the next run 50581 ... 50600.
  • With Hyper-V on, Windows reserves tcp ports in blocks of 100. This machine has eight such blocks in the dynamic range (netsh int ipv4 show excludedportrange protocol=tcp), one of them 50581-50680.
  • When the udp counter sits inside a reserved block, every one of the test's 20 attempts reads as taken on tcp, and it throws "every bound udp port also read as taken on tcp" in a few milliseconds. That matches the 14 ms in the report, which I wrongly read as too fast for the loop.

The test now goes the other way: it asks the OS for a free tcp port and holds that number on udp. A tcp port handed out by listen(0) is never in an excluded range. If the udp bind fails, it tries another port. While the udp socket holds the port, it also checks that the udp side reads as taken, so the test now checks both directions.

The first commit is still worth having on its own: every socket and server the tests create now has an error handler, so a failed bind fails its own test with its own message.

No change to isPortFree itself. The app checks each forward against the protocol it uses, and a reserved tcp port really can't be listened on, so reading it as taken is right.

Testing

  • npx vitest run tests/portCheck.test.ts 15 times in a row: all green
  • npm run typecheck
  • npm test: 20 files, 180 tests pass

@A21-devs A21-devs added the tests label Dec 5, 2026
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.
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.

@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. The cause in the description checks out: the udp counter runs in order and lands in a reserved tcp block, and going from tcp to udp avoids it. One small thing below, otherwise clean.

Comment thread tests/portCheck.test.ts Outdated
function bindUdp(port: number): Promise<Socket> {
return new Promise((resolve, reject) => {
const sock = createSocket('udp4')
sock.once('error', (err) => {

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.

This error listener stays on the socket after the bind succeeds. If the socket errors later in the test, the handler closes it and calls reject on a promise that already resolved, which does nothing, so the error disappears instead of failing the test. Take the listener off in the bind callback so later errors surface as unhandled again.

@A21-devs

A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner Author

Fixed in fe05087: bindUdp now takes its error listener off in the bind callback, so only the bind's own failure is turned into a rejection. Ran the file 5 more times, plus typecheck and the full npm test (180 pass).

@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.

Checked the follow-up commit: the bind's error listener is removed once the bind succeeds, so a later socket error is no longer swallowed. Nothing else open on this one.

Left on, it swallowed any later error on the socket instead of letting
it fail the test.
@A21-devs
A21-devs merged commit 340c52d into main Dec 5, 2026
1 check passed
@A21-devs
A21-devs deleted the tests/port-check-flake branch December 5, 2026 19:59
@A21-devs A21-devs mentioned this pull request Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: the tcp and udp port check test fails under load

1 participant