Skip to content

Commit

Permalink
Use closefrom() to close open file descriptors
Browse files Browse the repository at this point in the history
Replace the naive close() loop in js_os_exec with closefrom().

On my system RLIMIT_NOFILE is set to 1 million and the delay from the
loop gets noticeable when I spawn many processes.

Fixes: #711
  • Loading branch information
bnoordhuis committed Nov 21, 2024
1 parent 769e78f commit 70a6e6a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3123,18 +3123,20 @@ static JSValue js_os_exec(JSContext *ctx, JSValue this_val,
}
if (pid == 0) {
/* child */
int fd_max = sysconf(_SC_OPEN_MAX);

/* remap the stdin/stdout/stderr handles if necessary */
for(i = 0; i < 3; i++) {
if (std_fds[i] != i) {
if (dup2(std_fds[i], i) < 0)
_exit(127);
}
}

#if defined(__APPLE__)
int fd_max = sysconf(_SC_OPEN_MAX);
for(i = 3; i < fd_max; i++)
close(i);
#else
closefrom(3);
#endif
if (cwd) {
if (chdir(cwd) < 0)
_exit(127);
Expand Down

0 comments on commit 70a6e6a

Please sign in to comment.