Skip to content

Port not detected as busy when hosts don't match #50

@fvsch

Description

@fvsch

Starting a server on port X may result in an unreachable server when that port is already used by another service, as long as that service listens on a different host.

For instance, on macOS, running lsof -i -P | rg 'TCP' | rg '(LISTEN|ESTABLISHED)' may show:

Lunar     16386        florens    5u  IPv4 0x5c3816aa023bce6b      0t0    TCP localhost:23803 (LISTEN)
node      45275        florens   13u  IPv6 0xa2ac3e94b81f5a4e      0t0    TCP localhost:23803 (LISTEN)

The Lunar service runs on port 23803, and:

  1. Running servitsy --host localhost --port 23803 will not show an error, but the servitsy server will not be reachable.
  2. Running servitsy --host 127.0.0.1 --port 23803 does show an error.

It looks like Node's http.Server.listen does not emit an 'error' event for an already used port if the host is not the exact host (or IP?) used by another service.

Not sure how to work around this. Libraries that try to find a port seem to create a new net.Server and attempt to connect to it (e.g. https://github.com/http-party/node-portfinder/blob/master/lib/portfinder.js or https://github.com/samvv/node-find-free-ports/blob/main/package/index.ts). Could be costlier than the current solution, but who knows, it might be best?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions