Skip to content

Commit 5756f92

Browse files
committed
src: do platform-specific initialization earlier
Execute the per-platform initialization logic as early as possible, for two reasons: 1. It opens the way for an upcoming commit to simplify early SIGUSR1 handling. 2. It should make life easier for embedders because io.js no longer mucks around with the file descriptor limit or signal disposition of the process. PR-URL: #615 Reviewed-By: Sam Roberts <[email protected]>
1 parent 24bd4e0 commit 5756f92

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/node.cc

+32-29
Original file line numberDiff line numberDiff line change
@@ -3322,6 +3322,36 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
33223322
}
33233323

33243324

3325+
inline void PlatformInit() {
3326+
#ifdef __POSIX__
3327+
// Raise the open file descriptor limit.
3328+
struct rlimit lim;
3329+
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
3330+
// Do a binary search for the limit.
3331+
rlim_t min = lim.rlim_cur;
3332+
rlim_t max = 1 << 20;
3333+
// But if there's a defined upper bound, don't search, just set it.
3334+
if (lim.rlim_max != RLIM_INFINITY) {
3335+
min = lim.rlim_max;
3336+
max = lim.rlim_max;
3337+
}
3338+
do {
3339+
lim.rlim_cur = min + (max - min) / 2;
3340+
if (setrlimit(RLIMIT_NOFILE, &lim)) {
3341+
max = lim.rlim_cur;
3342+
} else {
3343+
min = lim.rlim_cur;
3344+
}
3345+
} while (min + 1 < max);
3346+
}
3347+
// Ignore SIGPIPE
3348+
RegisterSignalHandler(SIGPIPE, SIG_IGN);
3349+
RegisterSignalHandler(SIGINT, SignalExit, true);
3350+
RegisterSignalHandler(SIGTERM, SignalExit, true);
3351+
#endif // __POSIX__
3352+
}
3353+
3354+
33253355
void Init(int* argc,
33263356
const char** argv,
33273357
int* exec_argc,
@@ -3396,35 +3426,6 @@ void Init(int* argc,
33963426

33973427
V8::SetArrayBufferAllocator(&ArrayBufferAllocator::the_singleton);
33983428

3399-
#ifdef __POSIX__
3400-
// Raise the open file descriptor limit.
3401-
{ // NOLINT (whitespace/braces)
3402-
struct rlimit lim;
3403-
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
3404-
// Do a binary search for the limit.
3405-
rlim_t min = lim.rlim_cur;
3406-
rlim_t max = 1 << 20;
3407-
// But if there's a defined upper bound, don't search, just set it.
3408-
if (lim.rlim_max != RLIM_INFINITY) {
3409-
min = lim.rlim_max;
3410-
max = lim.rlim_max;
3411-
}
3412-
do {
3413-
lim.rlim_cur = min + (max - min) / 2;
3414-
if (setrlimit(RLIMIT_NOFILE, &lim)) {
3415-
max = lim.rlim_cur;
3416-
} else {
3417-
min = lim.rlim_cur;
3418-
}
3419-
} while (min + 1 < max);
3420-
}
3421-
}
3422-
// Ignore SIGPIPE
3423-
RegisterSignalHandler(SIGPIPE, SIG_IGN);
3424-
RegisterSignalHandler(SIGINT, SignalExit, true);
3425-
RegisterSignalHandler(SIGTERM, SignalExit, true);
3426-
#endif // __POSIX__
3427-
34283429
if (!use_debug_agent) {
34293430
RegisterDebugSignalHandler();
34303431
}
@@ -3610,6 +3611,8 @@ Environment* CreateEnvironment(Isolate* isolate,
36103611

36113612

36123613
int Start(int argc, char** argv) {
3614+
PlatformInit();
3615+
36133616
const char* replaceInvalid = secure_getenv("NODE_INVALID_UTF8");
36143617

36153618
if (replaceInvalid == nullptr)

0 commit comments

Comments
 (0)