Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
src: raise maximum file descriptor limit
Browse files Browse the repository at this point in the history
Do a binary search for the maximum RLIMIT_NOFILE.  Works around the
low, low limits on certain high, high-priced devices from Cupertino, CA.
  • Loading branch information
bnoordhuis committed Oct 3, 2013
1 parent f311963 commit 6820054
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#define umask _umask
typedef int mode_t;
#else
#include <sys/resource.h> // getrlimit, setrlimit
#include <unistd.h> // setuid, getuid
#endif

Expand Down Expand Up @@ -3103,6 +3104,28 @@ void Init(int* argc,
node_isolate = Isolate::GetCurrent();

#ifdef __POSIX__
// Raise the open file descriptor limit.
{
struct rlimit lim;
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
// Do a binary search for the limit.
rlim_t min = lim.rlim_cur;
rlim_t max = 1 << 20;
// But if there's a defined upper bound, don't search, just set it.
if (lim.rlim_max != RLIM_INFINITY) {
min = lim.rlim_max;
max = lim.rlim_max;
}
do {
lim.rlim_cur = min + (max - min) / 2;
if (setrlimit(RLIMIT_NOFILE, &lim)) {
max = lim.rlim_cur;
} else {
min = lim.rlim_cur;
}
} while (min + 1 < max);
}
}
// Ignore SIGPIPE
RegisterSignalHandler(SIGPIPE, SIG_IGN);
RegisterSignalHandler(SIGINT, SignalExit);
Expand Down

9 comments on commit 6820054

@ry
Copy link

@ry ry commented on 6820054 Oct 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be in libuv?

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit too special-purpose for that IMO.

@indutny
Copy link
Member

@indutny indutny commented on 6820054 Oct 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 for doing it in libuv, its too hacky for general users.

@ThisIsMissEm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing it in a separate dep, as a single function? increasenofilerlimit or something? That way people using libuv could also choose to use it, without needing to have to have it within libuv?

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 20 lines of code? Seems like overkill. If people want to use it, they have my permission to copy it. Not that they need permission, it being covered under the MIT license.

@ThisIsMissEm
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis I guess the same principles we apply to node modules we don't apply to C code then. :)

@igorcosta
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libuv? OMG seriously?

@bmeck
Copy link
Member

@bmeck bmeck commented on 6820054 Nov 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ruins expectations of ulimit -S not being automagically overridden when you run commands.

@rvagg
Copy link
Member

@rvagg rvagg commented on 6820054 Nov 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we commandline flag this perhaps? It does seem to violate rules about least-surprise.

Please sign in to comment.