Closed ayame113 closed 1 month ago
I tried debugging this.
This package first checks port availability in order for all hostnames returned by networkInterfaces()
.
At that time, the server created by net.createServer
will be reused.
The result is an EADDRINUSE error because it's listening as soon as it's closed (this is the behavior difference between Node.js and Deno).
This seems to make the portfinder package determine that the port is not available.
So the minimal code that demonstrates the difference in behavior between Node.js and Deno looks like this:
// import net from "https://deno.land/std@0.168.0/node/net.ts";
import net from "node:net";
const host1 = "0.0.0.0";
const host2 = null;
const server = net.createServer(() => {});
server.once("listening", () => {
// Listen on another host as soon as close the server.
server.close();
server.listen(8081, host2); // EADDRINUSE error occurs only in Deno!
});
server.listen(8081, host1);
Currently it's difficult to make server.close()
synchronous because the close()
method of flash (Deno.serve
) is asynchronous.
~~flash is now under a heavy rewrite. ref. https://github.com/denoland/deno/issues/17146 Maybe we can start considering adding synchronous shutting down API to it after the rewrite completed.~~
Update: Deno.serve
is unrelated to this issue. portfinder
uses net.Server
, which is shimmed by Deno.listen
and Deno.Listener
.
Now the error message is like the below:
error: Uncaught Error: listen UNKNOWN: unknown error 192.168.133.163:32561
at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:89:11)
at __node_internal_uvExceptionWithHostPort (ext:deno_node/internal/errors.ts:120:12)
at Server._setupListenHandle [as _listen2] (ext:deno_node/net.ts:1187:20)
at _listenInCluster (ext:deno_node/net.ts:1009:16)
at doListen (ext:deno_node/net.ts:1019:13)
at processTicksAndRejections (ext:deno_node/_next_tick.ts:34:29)
Just to mention this is probably the reason why npm:http-server
also does not work unless --port
is specified.
Will fix as part of https://github.com/denoland/deno_core/pull/440
I'm having trouble running portfinder, a package that finds available ports, on Deno.
Running the code below in Node.js correctly finds available ports.
However, when I run it on Deno, I get the following error:
This problem should affect many packages that depend on portfinder.