Skip to content

Commit 20ffdf7

Browse files
[3.14] On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (GH-160210) (#160221)
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 260ee94 commit 20ffdf7

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
@@ -685,6 +685,24 @@ def fd_count():
685685
except FileNotFoundError:
686686
pass
687687

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

733751
return count
734752

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

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

0 commit comments

Comments
 (0)