Skip to content

Speed up close_fds with the new close_range() Linux/FreeBSD syscall #189

Open
@nh2

Description

Background:

As written in

close_fds :: Bool, -- ^ Close all file descriptors except stdin, stdout and stderr in the new process (on Windows, only works if std_in, std_out, and std_err are all Inherit). This implementation will call close an every fd from 3 to the maximum of open files, which can be slow for high maximum of open files.

This implementation will call close() an every fd from 3 to the maximum of open files, which can be slow for high maximum of open files.

The new close_range() syscall solves this, closing them all in 1 go. According to the LWN link, it is very fast, and you can give it MAXINT.

The code that needs to be augmented (with CPP):

if (close_fds) {
int i;
if (max_fd == 0) {
#if HAVE_SYSCONF
max_fd = sysconf(_SC_OPEN_MAX);
if (max_fd == -1) {
max_fd = 256;
}
#else
max_fd = 256;
#endif
}
// XXX Not the pipe
for (i = 3; i < max_fd; i++) {
if (i != forkCommunicationFds[1]) {
close(i);
}
}
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions