http-party / node-portfinder

A simple tool to find an open port or domain socket on the current machine
https://github.com/http-party/node-portfinder
Other
887 stars 95 forks source link

Free port still in use #104

Closed sla89 closed 4 years ago

sla89 commented 4 years ago

Hello together,

I am facing the problem that portfinder is blocking a free port because the callback is called directly after calling server.close().

The problem is that server.close() is an asynchronous operation and as long as the close event is not emitted the port is still used by the server (Node API docs: https://nodejs.org/docs/latest/api/net.html#net_server_close_callback):

server.close([callback])[src]#
Added in: v0.1.90
callback <Function> Called when the server is closed
Returns: <net.Server>
Stops the server from accepting new connections and keeps existing connections.
This function is asynchronous, the server is finally closed when all connections are
ended and the server emits a 'close' event. The optional callback will be called once
the 'close' event occurs. Unlike that event, it will be called with an Error as its only
argument if the server was not open when it was closed.

This is where the problem happens: https://github.com/http-party/node-portfinder/blob/master/lib/portfinder.js#L42

With regards, Stefan

rathboma commented 4 years ago

I am also having this issue as I am immediately starting a server to provide SSH tunnel access, and every now and then it fails due to the port not being available.

eriktrom commented 4 years ago

EDIT: see #105 for feedback on this issue, fyi

eriktrom commented 4 years ago

closing to cleanup issue list, comments or questions still welcome, if needed