From d7e0e32e3faaef94ad96fcbb239942ab6beb9d0a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 10 Oct 2023 17:38:48 +0530 Subject: [PATCH 1/5] Enable reuseport on macOS --- ext/net/ops.rs | 2 +- ext/node/polyfills/internal_binding/tcp_wrap.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 5738620f8cf72c..180a2bbbe2312c 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -378,7 +378,7 @@ where #[cfg(not(windows))] socket.set_reuse_address(true)?; if reuse_port { - #[cfg(target_os = "linux")] + #[cfg(not(windows))] socket.set_reuse_port(true)?; } let socket_addr = socket2::SockAddr::from(addr); diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index 3725b632507b2c..0c9b3a19162414 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -204,6 +204,7 @@ export class TCP extends ConnectionWrap { hostname: this.#address!, port: this.#port!, transport: "tcp" as const, + reusePort: true, }; let listener; From 8d0cbdcaba08a759aadd146840bff067a173d8bd Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 10 Oct 2023 18:13:03 +0530 Subject: [PATCH 2/5] get around unstable check --- ext/net/01_net.js | 4 ++-- ext/net/ops.rs | 4 +++- ext/node/polyfills/internal_binding/tcp_wrap.ts | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ext/net/01_net.js b/ext/net/01_net.js index f2bf5e7dfa2c64..94544382165ba6 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -419,13 +419,13 @@ class Datagram { const listenOptionApiName = Symbol("listenOptionApiName"); -function listen(args) { +function listen(args, checkUnstable = true) { switch (args.transport ?? "tcp") { case "tcp": { const { 0: rid, 1: addr } = ops.op_net_listen_tcp({ hostname: args.hostname ?? "0.0.0.0", port: Number(args.port), - }, args.reusePort); + }, args.reusePort, checkUnstable); addr.transport = "tcp"; return new Listener(rid, addr); } diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 180a2bbbe2312c..f2ab49c81f683f 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -356,11 +356,13 @@ pub fn op_net_listen_tcp( state: &mut OpState, #[serde] addr: IpAddr, reuse_port: bool, + check_unstable: bool, ) -> Result<(ResourceId, IpAddr), AnyError> where NP: NetPermissions + 'static, { - if reuse_port { + // Do not check for unstable if used by internal Node code, + if check_unstable && reuse_port { super::check_unstable(state, "Deno.listen({ reusePort: true })"); } state diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index 0c9b3a19162414..547f050f6c9529 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -45,6 +45,7 @@ import { INITIAL_ACCEPT_BACKOFF_DELAY, MAX_ACCEPT_BACKOFF_DELAY, } from "ext:deno_node/internal_binding/_listen.ts"; +import { listen } from "ext:deno_net/01_net.js"; /** The type of TCP socket. */ enum socketType { @@ -210,7 +211,7 @@ export class TCP extends ConnectionWrap { let listener; try { - listener = Deno.listen(listenOptions); + listener = listen(listenOptions, false); } catch (e) { if (e instanceof Deno.errors.AddrInUse) { return codeMap.get("EADDRINUSE")!; From 2b8bab0dd3ed1ae89647e040009d3f8567ab1eaf Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 10 Oct 2023 18:17:54 +0530 Subject: [PATCH 3/5] fmt --- ext/net/01_net.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 94544382165ba6..b4c6e2750d5396 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -422,10 +422,14 @@ const listenOptionApiName = Symbol("listenOptionApiName"); function listen(args, checkUnstable = true) { switch (args.transport ?? "tcp") { case "tcp": { - const { 0: rid, 1: addr } = ops.op_net_listen_tcp({ - hostname: args.hostname ?? "0.0.0.0", - port: Number(args.port), - }, args.reusePort, checkUnstable); + const { 0: rid, 1: addr } = ops.op_net_listen_tcp( + { + hostname: args.hostname ?? "0.0.0.0", + port: Number(args.port), + }, + args.reusePort, + checkUnstable, + ); addr.transport = "tcp"; return new Listener(rid, addr); } From 0935e7d5e5cc9c77016423e8bf4cf64351cd455a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 10 Oct 2023 20:37:21 +0530 Subject: [PATCH 4/5] do not change public API --- ext/net/01_net.js | 7 ++++++- ext/node/polyfills/internal_binding/tcp_wrap.ts | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ext/net/01_net.js b/ext/net/01_net.js index b4c6e2750d5396..3256035a56ec26 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -419,7 +419,11 @@ class Datagram { const listenOptionApiName = Symbol("listenOptionApiName"); -function listen(args, checkUnstable = true) { +function listen(args) { + return listenInternal(args, true); +} + +function listenInternal(args, checkUnstable = true) { switch (args.transport ?? "tcp") { case "tcp": { const { 0: rid, 1: addr } = ops.op_net_listen_tcp( @@ -515,6 +519,7 @@ export { Datagram, listen, Listener, + listenInternal, listenOptionApiName, resolveDns, shutdown, diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index 547f050f6c9529..21dfbf0eadfc1f 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -45,7 +45,7 @@ import { INITIAL_ACCEPT_BACKOFF_DELAY, MAX_ACCEPT_BACKOFF_DELAY, } from "ext:deno_node/internal_binding/_listen.ts"; -import { listen } from "ext:deno_net/01_net.js"; +import { listenInternal } from "ext:deno_net/01_net.js"; /** The type of TCP socket. */ enum socketType { @@ -211,7 +211,7 @@ export class TCP extends ConnectionWrap { let listener; try { - listener = listen(listenOptions, false); + listener = listenInternal(listenOptions, false); } catch (e) { if (e instanceof Deno.errors.AddrInUse) { return codeMap.get("EADDRINUSE")!; From 6744b9d71e773b654767c0c8e73929562b4278e7 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 11 Oct 2023 08:27:23 +0530 Subject: [PATCH 5/5] update test --- cli/tests/unit/net_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts index 2a98b5e26f3e0a..a6803e20447c82 100644 --- a/cli/tests/unit/net_test.ts +++ b/cli/tests/unit/net_test.ts @@ -1224,7 +1224,7 @@ Deno.test({ }); Deno.test({ - ignore: Deno.build.os === "linux", + ignore: Deno.build.os === "linux" || Deno.build.os === "darwin", permissions: { net: true }, }, function netTcpListenReusePortDoesNothing() { const listener1 = Deno.listen({ port: 4003, reusePort: true });