Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): enable reuseport for net Socket on macOS #20869

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
17 changes: 13 additions & 4 deletions ext/net/01_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,20 @@ class Datagram {
const listenOptionApiName = Symbol("listenOptionApiName");

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({
hostname: args.hostname ?? "0.0.0.0",
port: Number(args.port),
}, args.reusePort);
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);
}
Expand Down Expand Up @@ -511,6 +519,7 @@ export {
Datagram,
listen,
Listener,
listenInternal,
listenOptionApiName,
resolveDns,
shutdown,
Expand Down
6 changes: 4 additions & 2 deletions ext/net/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ pub fn op_net_listen_tcp<NP>(
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
Expand All @@ -378,7 +380,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);
Expand Down
4 changes: 3 additions & 1 deletion ext/node/polyfills/internal_binding/tcp_wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
INITIAL_ACCEPT_BACKOFF_DELAY,
MAX_ACCEPT_BACKOFF_DELAY,
} from "ext:deno_node/internal_binding/_listen.ts";
import { listenInternal } from "ext:deno_net/01_net.js";

/** The type of TCP socket. */
enum socketType {
Expand Down Expand Up @@ -204,12 +205,13 @@ export class TCP extends ConnectionWrap {
hostname: this.#address!,
port: this.#port!,
transport: "tcp" as const,
reusePort: true,
};

let listener;

try {
listener = Deno.listen(listenOptions);
listener = listenInternal(listenOptions, false);
} catch (e) {
if (e instanceof Deno.errors.AddrInUse) {
return codeMap.get("EADDRINUSE")!;
Expand Down
Loading