Skip to content

Commit 2ba1402

Browse files
authored
Merge pull request #347 from DeterminateSystems/bump-file-limit
Increase the open file soft limit to the hard limit
2 parents 2acd0d1 + 083d28c commit 2ba1402

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libmain/shared.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cstdlib>
1616
#include <sys/time.h>
1717
#include <sys/stat.h>
18+
#include <sys/resource.h>
1819
#include <unistd.h>
1920
#include <signal.h>
2021
#ifdef __linux__
@@ -116,6 +117,26 @@ std::string getArg(const std::string & opt, Strings::iterator & i, const Strings
116117
static void sigHandler(int signo) {}
117118
#endif
118119

120+
/**
121+
* Increase the open file soft limit to the hard limit. On some
122+
* platforms (macOS), the default soft limit is very low, but the hard
123+
* limit is high. So let's just raise it the maximum permitted.
124+
*/
125+
void bumpFileLimit()
126+
{
127+
#ifndef _WIN32
128+
struct rlimit limit;
129+
if (getrlimit(RLIMIT_NOFILE, &limit) != 0)
130+
return;
131+
132+
if (limit.rlim_cur < limit.rlim_max) {
133+
limit.rlim_cur = limit.rlim_max;
134+
// Ignore errors, this is best effort.
135+
setrlimit(RLIMIT_NOFILE, &limit);
136+
}
137+
#endif
138+
}
139+
119140
void initNix(bool loadConfig)
120141
{
121142
/* Turn on buffering for cerr. */
@@ -183,6 +204,8 @@ void initNix(bool loadConfig)
183204
now. In particular, store objects should be readable by
184205
everybody. */
185206
umask(0022);
207+
208+
bumpFileLimit();
186209
}
187210

188211
LegacyArgs::LegacyArgs(

0 commit comments

Comments
 (0)