Skip to content

Commit b3e6eb1

Browse files
[3.15] On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (GH-160210) (#160220)
On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (GH-160210) fd_count() can now use /dev/fd/ fdescfs file system if it's mounted, which is not the case by default. Otherwise, call os.dup(fd) in a loop which is slow since os.sysconf("SC_OPEN_MAX") is larger than 200,000 by default. (cherry picked from commit 226ca06) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 253090f commit b3e6eb1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎Lib/test/support/os_helper.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,24 @@ def fd_count():
686686
except FileNotFoundError:
687687
pass
688688

689+
if sys.platform == "freebsd" and fd_count._dev_fd_mounted:
690+
# Check if /dev/fd/ fdescfs file system is mounted. By default, it's
691+
# not mounted and /dev/fd/ always contains 3 files (0, 1, 2).
692+
try:
693+
dirfd = os.open("/dev/fd", os.O_RDONLY)
694+
try:
695+
if os.path.exists(f"/dev/fd/{dirfd}"):
696+
names = list(os.scandir(dirfd))
697+
# Don't count dirfd
698+
return len(names) - 1
699+
else:
700+
# fdescfs is not mounted
701+
fd_count._dev_fd_mounted = False
702+
finally:
703+
os.close(dirfd)
704+
except FileNotFoundError:
705+
pass
706+
689707
MAXFD = 256
690708
if hasattr(os, 'sysconf'):
691709
try:
@@ -733,6 +751,9 @@ def fd_count():
733751

734752
return count
735753

754+
# On FreeBSD, flag set to False if /dev/fd/ is not mounted as fdescfs
755+
fd_count._dev_fd_mounted = True
756+
736757

737758
if hasattr(os, "umask"):
738759
@contextlib.contextmanager

0 commit comments

Comments
 (0)