diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index cac9e38ad857..815c60cf82f3 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #ifdef __linux__ @@ -116,6 +117,26 @@ std::string getArg(const std::string & opt, Strings::iterator & i, const Strings static void sigHandler(int signo) {} #endif +/** + * Increase the open file soft limit to the hard limit. On some + * platforms (macOS), the default soft limit is very low, but the hard + * limit is high. So let's just raise it the maximum permitted. + */ +void bumpFileLimit() +{ +#ifndef _WIN32 + struct rlimit limit; + if (getrlimit(RLIMIT_NOFILE, &limit) != 0) + return; + + if (limit.rlim_cur < limit.rlim_max) { + limit.rlim_cur = limit.rlim_max; + // Ignore errors, this is best effort. + setrlimit(RLIMIT_NOFILE, &limit); + } +#endif +} + void initNix(bool loadConfig) { /* Turn on buffering for cerr. */ @@ -183,6 +204,8 @@ void initNix(bool loadConfig) now. In particular, store objects should be readable by everybody. */ umask(0022); + + bumpFileLimit(); } LegacyArgs::LegacyArgs(