Repository navigation
Port check test: stop landing in Hyper-V's reserved tcp ports - #161
Conversation
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
left a comment
There was a problem hiding this comment.
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.
| function bindUdp(port: number): Promise<Socket> { | ||
| return new Promise((resolve, reject) => { | ||
| const sock = createSocket('udp4') | ||
| sock.once('error', (err) => { |
There was a problem hiding this comment.
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.
|
Fixed in fe05087: |
A21-devs
left a comment
There was a problem hiding this comment.
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.
Closes #154
The guess in the issue (an unhandled bind error) turned out not to be the cause. What happens:
netsh int ipv4 show excludedportrange protocol=tcp), one of them 50581-50680.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
errorhandler, so a failed bind fails its own test with its own message.No change to
isPortFreeitself. 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.ts15 times in a row: all greennpm run typechecknpm test: 20 files, 180 tests pass