-
Notifications
You must be signed in to change notification settings - Fork 0
Description
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:
- Running
servitsy --host localhost --port 23803will not show an error, but the servitsy server will not be reachable. - Running
servitsy --host 127.0.0.1 --port 23803does 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?