Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Prevent error when scanning a large range of ports #2

baalexander opened this Issue · 4 comments

2 participants


When scanning hundreds of ports asynchronously, an error would occur when trying to use one of the ports that returned with status 'closed'. The problem goes away when 50 or so ports are scanned at a time.


Thanks for a nice module.

Is there a reliable chunk I can scan at a time to make sure I won't get errors? Do you know what the limit is and what it depends on?


Hi @jacobrask, are you getting the errors currently? If so, do you have some sample code that gets the error?

I was getting the error when using findAPortNotInUse with a large range (> 500), so I switched to a port range of 50 (portscanner.findAPortNotInUse(9000, 9050, hostname, function(error, port) {})) for my batch size and it's been working.

However, I just tried reproducing the issue by modifying the example code like so:

  // Finds a port no service is listening on
  portscanner.findAPortNotInUse(3000, 100000, 'localhost', function(error, port) {
    // The order is unknown as the port status checks are asynchronous.
    console.log('Found a closed port at ' + port)

and could not reproduce this error.

The issue may have been from something else in my actual project code (not the sample). I'll test my project code tonight with the larger ranges to see if I can reproduce it there.


I got

Error: EMFILE, Too many open files

when specifying a range of 1018 ports or more on my workstation.

portscanner.findAPortNotInUse 49200, 50218, address, (err, port) ->
    throw err if err
    server.listen port, address, ->
        console.log "Web server listening on #{address}:#{port}"
        callback null, { port: port, address: address }

I don't really need that big of a range obviously, but it's still a limitation, and if it depends on something unknown maybe the error will trigger on fewer under some other circumstances...

@baalexander baalexander referenced this issue from a commit
@baalexander Checks range of ports one at a time.
Uses the [async]( library to conveniently check
each port serially. This may be slower, but prevents errors related to too many
open socket connections.

See Issue #2.

Originally the scanner checked every port in the range at once and the first port to return with a matching status was returned in the find a port functions. The problem with that approach was too many socket connections seemed to cause errors that depended on a number of things, including the host.

I switched the scanner to use the async library and check one port at a time. This prevented the too many open socket error for me locally. Does this resolve your error @jacobrask?

@baalexander baalexander referenced this issue from a commit
@baalexander Updates to v0.1.1.
Uses the async library and resolves Issue #2 when scanning too many ports.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.