Skip to content

Commit bbdf636

Browse files
committed
server: REUSEPORT is not allowed on unix socket
see torvalds/linux@5b0af62
1 parent 482ed8d commit bbdf636

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/server.zig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,19 +498,22 @@ fn isUnixSocket(addr: std.net.Address) bool {
498498
}
499499

500500
pub fn listen(address: std.net.Address) !std.posix.socket_t {
501+
const isunixsock = isUnixSocket(address);
501502

502503
// create socket
503504
const flags = std.posix.SOCK.STREAM | std.posix.SOCK.CLOEXEC | std.posix.SOCK.NONBLOCK;
504-
const proto = if (isUnixSocket(address)) @as(u32, 0) else std.posix.IPPROTO.TCP;
505+
const proto = if (isunixsock) @as(u32, 0) else std.posix.IPPROTO.TCP;
505506
const sockfd = try std.posix.socket(address.any.family, flags, proto);
506507
errdefer std.posix.close(sockfd);
507508

508509
// socket options
509-
if (@hasDecl(std.posix.SO, "REUSEPORT")) {
510+
//
511+
// REUSEPORT can't be set on unix socket anymore.
512+
// see https://github.com/torvalds/linux/commit/5b0af621c3f6ef9261cf6067812f2fd9943acb4b
513+
if (@hasDecl(std.posix.SO, "REUSEPORT") and !isunixsock) {
510514
try setSockOpt(sockfd, std.posix.SOL.SOCKET, std.posix.SO.REUSEPORT, 1);
511-
} else {
512-
try setSockOpt(sockfd, std.posix.SOL.SOCKET, std.posix.SO.REUSEADDR, 1);
513515
}
516+
try setSockOpt(sockfd, std.posix.SOL.SOCKET, std.posix.SO.REUSEADDR, 1);
514517
if (!isUnixSocket(address)) {
515518
if (builtin.target.os.tag == .linux) { // posix.TCP not available on MacOS
516519
// WARNING: disable Nagle's alogrithm to avoid latency issues

0 commit comments

Comments
 (0)