Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid crashing if all ports above the start port are unavailable #77

wants to merge 1 commit into
base: master


None yet
1 participant
Copy link

commented May 29, 2019

Right now, calling getPort when every port above the given port is in use will throw:

RangeError [ERR_SOCKET_BAD_PORT]: Port should be >= 0 and < 65536. Received 65536.

This isn't passed to the callback - it's thrown directly by node. Because this isn't handled at all that will usually crash the whole node process.

To easily reproduce this, start a server on port 65535 (the maximum port), and then run portfinder.getPortPromise({ port: 65535 }).

This happens because testPort is unbounded, and constantly tries ports until it finds an available one, without checking whether the port to test is valid, so when 65535 is in use it tries to test 65536, which isn't a valid port.

This PR makes testPort instead check whether the next port is valid before testing it, so that it fails cleanly if there are no ports available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.