Find function does not stop testing ports #13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a problem I found while using https://www.npmjs.com/package/proxy-chain.
Explanation
The proxy-chain module has a function to create a proxy server using a random open port, it finds the port using portastic. One of the ports that was tested gave an error and my PC would freeze for a few seconds after making the proxy server.
I am not sure why my PC freezes, but it is a result of the rejection.
The rejection comes from portastic. It would reject on errors that would occure when opening a server. It rejects after the proxy server is made, so something was messing things up in the background while the server was working fine.
The rejection comes from
node-portastic/lib/portastic.js
Line 37 in d27c7d4
node-portastic/lib/portastic.js
Line 53 in d27c7d4
The rejection is fine, it is just because it is not caught. But when using the find function all exceptions should be caught anyway. I don't change that in this pull request, because I don't think it is that big of a problem.
The biggest issue, and the reason for this pull request, is that the find function does not cancel when it finds the asked amount of open ports. Proxy-chain tells portastic to find a single open port in the range 20000 to 60000, but portastic fails to stop testing ports / cancel the promise.
I have replaced it with a recursive function, that way it is easier to control when the find function should stop, and actually make it stop.
Replicate
To replicate the problem use the following code:
It will find the first open port and resolve the promise, but it will keep testing. See the image below for the debug log:
With my fix: