[Python-checkins] bpo-38061: os.closerange() uses closefrom() on FreeBSD (GH-19696)

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


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

bpo-38061: os.closerange() uses closefrom() on FreeBSD (GH-19696)

On FreeBSD, os.closerange(fd_low, fd_high) now calls
closefrom(fd_low) if fd_high is greater than or equal to
sysconf(_SC_OPEN_MAX).

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-27-08.bpo-38061.cdlkMz.rst
M Modules/posixmodule.c

diff --git a/Misc/NEWS.d/next/Library/2020-04-24-01-27-08.bpo-38061.cdlkMz.rst b/Misc/NEWS.d/next/Library/2020-04-24-01-27-08.bpo-38061.cdlkMz.rst
new file mode 100644
index 0000000000000..e55d5d50bd7e2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-24-01-27-08.bpo-38061.cdlkMz.rst
@@ -0,0 +1,6 @@
+On FreeBSD, ``os.closerange(fd_low, fd_high)`` now calls ``closefrom(fd_low)``
+if *fd_high* is greater than or equal to ``sysconf(_SC_OPEN_MAX)``.
+
+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/posixmodule.c b/Modules/posixmodule.c
index 3386be0fbc85a..3d3f6ac969926 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -8687,10 +8687,13 @@ _fdwalk_close_func(void *lohi, int fd)
     int lo = ((int *)lohi)[0];
     int hi = ((int *)lohi)[1];
 
-    if (fd >= hi)
+    if (fd >= hi) {
         return 1;
-    else if (fd >= lo)
-        close(fd);
+    }
+    else if (fd >= lo) {
+        /* Ignore errors */
+        (void)close(fd);
+    }
     return 0;
 }
 #endif /* HAVE_FDWALK */
@@ -8711,8 +8714,6 @@ os_closerange_impl(PyObject *module, int fd_low, int fd_high)
 {
 #ifdef HAVE_FDWALK
     int lohi[2];
-#else
-    int i;
 #endif
     Py_BEGIN_ALLOW_THREADS
     _Py_BEGIN_SUPPRESS_IPH
@@ -8721,8 +8722,20 @@ os_closerange_impl(PyObject *module, int fd_low, int fd_high)
     lohi[1] = fd_high;
     fdwalk(_fdwalk_close_func, lohi);
 #else
-    for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
-        close(i);
+    fd_low = Py_MAX(fd_low, 0);
+#ifdef __FreeBSD__
+    if (fd_high >= sysconf(_SC_OPEN_MAX)) {
+        /* Any errors encountered while closing file descriptors are ignored */
+        closefrom(fd_low);
+    }
+    else
+#endif
+    {
+        for (int i = fd_low; i < fd_high; i++) {
+            /* Ignore errors */
+            (void)close(i);
+        }
+    }
 #endif
     _Py_END_SUPPRESS_IPH
     Py_END_ALLOW_THREADS



More information about the Python-checkins mailing list