From 8dda64fa3e1d3249baa948dc0558e3f65d71e6df Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 3 Nov 2020 00:36:28 +0100 Subject: [PATCH] Fix Darwin codepath On Darwin, according to the man pages for setrlimit(), when adjusting max number of open fds, the reported hard max by getrlimit() is only theoretical, while the actual maximum, set in the kernel, is hardcoded in the header file. Therefore, the reported max has to be adjusted as `min(OPEN_MAX, lim.max)`. Signed-off-by: Jakub Konka --- lib/std/os/bits/darwin.zig | 3 +++ src/main.zig | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/std/os/bits/darwin.zig b/lib/std/os/bits/darwin.zig index a30d11fcd12ee3d0a1328c636d7462f0c2e322b2..3e4149decd08b438b4d2ccf285d7a0ad9dc0b4a3 100644 --- a/lib/std/os/bits/darwin.zig +++ b/lib/std/os/bits/darwin.zig @@ -1475,6 +1475,9 @@ pub const CLOCK_UPTIME_RAW_APPROX = 9; pub const CLOCK_PROCESS_CPUTIME_ID = 12; pub const CLOCK_THREAD_CPUTIME_ID = 16; +/// Max open files per process +/// https://opensource.apple.com/source/xnu/xnu-4903.221.2/bsd/sys/syslimits.h.auto.html +pub const OPEN_MAX = 10240; pub const RUSAGE_SELF = 0; pub const RUSAGE_CHILDREN = -1; diff --git a/src/main.zig b/src/main.zig index 551f168683e43986c6c9da5da10f2327b01c049c..90a0aba46084cb8286a08bd41846565a3191869e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2986,6 +2986,14 @@ fn gimmeMoreOfThoseSweetSweetFileDescriptors() void { const posix = std.os; var lim = posix.getrlimit(.NOFILE) catch return; // Oh well; we tried. + if (std.Target.current.isDarwin()) { + // On Darwin, `NOFILE` is bounded by a hardcoded value `OPEN_MAX`. + // According to the man pages for setrlimit(): + // setrlimit() now returns with errno set to EINVAL in places that historically succeeded. + // It no longer accepts "rlim_cur = RLIM_INFINITY" for RLIM_NOFILE. + // Use "rlim_cur = min(OPEN_MAX, rlim_max)". + lim.max = std.math.min(posix.darwin.OPEN_MAX, lim.max); + } if (lim.cur == lim.max) return; // Do a binary search for the limit. -- 2.54.0