From 12a9e0f4150f0d30c851727f4dc2354fa6c90682 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Feb 2024 17:03:06 -0700 Subject: [PATCH] std.net.listen: fix Windows API use In a previous commit I removed a load-bearing use of `@hasDecl` to detect whether the SO.REUSEPORT option should be set. `@hasDecl` should not be used for OS feature detection because it can hide bugs. The new logic checks for the operating system specifically and then does the thing that is supposed to be done on that operating system directly. --- lib/std/net.zig | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index f7e19850d3..3f2f4c3f6c 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -217,7 +217,10 @@ pub const Address = extern union { /// If more than this many connections pool in the kernel, clients will start /// seeing "Connection refused". kernel_backlog: u31 = 128, + /// Sets SO_REUSEADDR and SO_REUSEPORT on POSIX. + /// Sets SO_REUSEADDR on Windows, which is roughly equivalent. reuse_address: bool = false, + /// Deprecated. Does nothing. reuse_port: bool = false, force_nonblocking: bool = false, }; @@ -242,15 +245,15 @@ pub const Address = extern union { posix.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)), ); - } - - if (options.reuse_port) { - try posix.setsockopt( - sockfd, - posix.SOL.SOCKET, - posix.SO.REUSEPORT, - &mem.toBytes(@as(c_int, 1)), - ); + switch (builtin.os.tag) { + .windows => {}, + else => try posix.setsockopt( + sockfd, + posix.SOL.SOCKET, + posix.SO.REUSEPORT, + &mem.toBytes(@as(c_int, 1)), + ), + } } var socklen = address.getOsSockLen();