[Python-checkins] bpo-38061: subprocess uses closefrom() on FreeBSD (GH-19697)

Victor Stinner webhook-mailer at python.org
Fri Apr 24 06:07:03 EDT 2020


https://github.com/python/cpython/commit/e6f8abd500751a834b6fff4f107ecbd29f2184fe
commit: e6f8abd500751a834b6fff4f107ecbd29f2184fe
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-04-24T12:06:58+02:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2020-04-24-01-55-00.bpo-38061.XmULB3.rst
M Doc/whatsnew/3.9.rst
M Modules/_posixsubprocess.c

diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index ee851706055a3..325121df13981 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -471,6 +471,10 @@ Optimizations
   until the main thread handles signals.
   (Contributed by Victor Stinner in :issue:`40010`.)
 
+* Optimize the :mod:`subprocess` module on FreeBSD using ``closefrom()``.
+  (Contributed by Ed Maste, Conrad Meyer, Kyle Evans, Kubilay Kocak and Victor
+  Stinner in :issue:`38061`.)
+
 
 Build and C API Changes
 =======================
diff --git a/Misc/NEWS.d/next/Library/2020-04-24-01-55-00.bpo-38061.XmULB3.rst b/Misc/NEWS.d/next/Library/2020-04-24-01-55-00.bpo-38061.XmULB3.rst
new file mode 100644
index 0000000000000..603d80b88b074
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-24-01-55-00.bpo-38061.XmULB3.rst
@@ -0,0 +1,11 @@
+Optimize the :mod:`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
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index 7d5a7fe17c075..60dd78d92a4f5 100644
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -264,9 +264,15 @@ _close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
         start_fd = keep_fd + 1;
     }
     if (start_fd <= end_fd) {
+#if defined(__FreeBSD__)
+        /* Any errors encountered while closing file descriptors are ignored */
+        closefrom(start_fd);
+#else
         for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
-            close(fd_num);
+            /* Ignore errors */
+            (void)close(fd_num);
         }
+#endif
     }
 }
 



More information about the Python-checkins mailing list