Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6f8abd

Browse files
authored
bpo-38061: subprocess uses closefrom() on FreeBSD (GH-19697)
Optimize the subprocess module on FreeBSD using closefrom(). A single close(fd) syscall is cheap, but when sysconf(_SC_OPEN_MAX) is high, the loop calling close(fd) on each file descriptor can take several milliseconds. The workaround on FreeBSD to improve performance was to load and mount the fdescfs kernel module, but this is not enabled by default. Initial patch by Ed Maste (emaste), Conrad Meyer (cem), Kyle Evans (kevans) and Kubilay Kocak (koobs): https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=242274
1 parent 162c567 commit e6f8abd

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Doc/whatsnew/3.9.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ Optimizations
471471
until the main thread handles signals.
472472
(Contributed by Victor Stinner in :issue:`40010`.)
473473

474+
* Optimize the :mod:`subprocess` module on FreeBSD using ``closefrom()``.
475+
(Contributed by Ed Maste, Conrad Meyer, Kyle Evans, Kubilay Kocak and Victor
476+
Stinner in :issue:`38061`.)
477+
474478

475479
Build and C API Changes
476480
=======================
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Optimize the :mod:`subprocess` module on FreeBSD using ``closefrom()``.
2+
A single ``close(fd)`` syscall is cheap, but when ``sysconf(_SC_OPEN_MAX)`` is
3+
high, the loop calling ``close(fd)`` on each file descriptor can take several
4+
milliseconds.
5+
6+
The workaround on FreeBSD to improve performance was to load and mount the
7+
fdescfs kernel module, but this is not enabled by default.
8+
9+
Initial patch by Ed Maste (emaste), Conrad Meyer (cem), Kyle Evans (kevans) and
10+
Kubilay Kocak (koobs):
11+
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=242274

Modules/_posixsubprocess.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,15 @@ _close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
264264
start_fd = keep_fd + 1;
265265
}
266266
if (start_fd <= end_fd) {
267+
#if defined(__FreeBSD__)
268+
/* Any errors encountered while closing file descriptors are ignored */
269+
closefrom(start_fd);
270+
#else
267271
for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
268-
close(fd_num);
272+
/* Ignore errors */
273+
(void)close(fd_num);
269274
}
275+
#endif
270276
}
271277
}
272278

0 commit comments

Comments
 (0)