Skip to content

Commit 162c567

Browse files
authored
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
1 parent 4cc4d60 commit 162c567

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
On FreeBSD, ``os.closerange(fd_low, fd_high)`` now calls ``closefrom(fd_low)``
2+
if *fd_high* is greater than or equal to ``sysconf(_SC_OPEN_MAX)``.
3+
4+
Initial patch by Ed Maste (emaste), Conrad Meyer (cem), Kyle Evans (kevans)
5+
and Kubilay Kocak (koobs):
6+
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=242274

Modules/posixmodule.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8687,10 +8687,13 @@ _fdwalk_close_func(void *lohi, int fd)
86878687
int lo = ((int *)lohi)[0];
86888688
int hi = ((int *)lohi)[1];
86898689

8690-
if (fd >= hi)
8690+
if (fd >= hi) {
86918691
return 1;
8692-
else if (fd >= lo)
8693-
close(fd);
8692+
}
8693+
else if (fd >= lo) {
8694+
/* Ignore errors */
8695+
(void)close(fd);
8696+
}
86948697
return 0;
86958698
}
86968699
#endif /* HAVE_FDWALK */
@@ -8711,8 +8714,6 @@ os_closerange_impl(PyObject *module, int fd_low, int fd_high)
87118714
{
87128715
#ifdef HAVE_FDWALK
87138716
int lohi[2];
8714-
#else
8715-
int i;
87168717
#endif
87178718
Py_BEGIN_ALLOW_THREADS
87188719
_Py_BEGIN_SUPPRESS_IPH
@@ -8721,8 +8722,20 @@ os_closerange_impl(PyObject *module, int fd_low, int fd_high)
87218722
lohi[1] = fd_high;
87228723
fdwalk(_fdwalk_close_func, lohi);
87238724
#else
8724-
for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
8725-
close(i);
8725+
fd_low = Py_MAX(fd_low, 0);
8726+
#ifdef __FreeBSD__
8727+
if (fd_high >= sysconf(_SC_OPEN_MAX)) {
8728+
/* Any errors encountered while closing file descriptors are ignored */
8729+
closefrom(fd_low);
8730+
}
8731+
else
8732+
#endif
8733+
{
8734+
for (int i = fd_low; i < fd_high; i++) {
8735+
/* Ignore errors */
8736+
(void)close(i);
8737+
}
8738+
}
87268739
#endif
87278740
_Py_END_SUPPRESS_IPH
87288741
Py_END_ALLOW_THREADS

0 commit comments

Comments
 (0)