Skip to content

Commit 226ca06

Browse files
authored
On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (#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.
1 parent 3b3c692 commit 226ca06

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
@@ -688,6 +688,24 @@ def fd_count():
688688
except FileNotFoundError:
689689
pass
690690

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

736754
return count
737755

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

739760
if hasattr(os, "umask"):
740761
@contextlib.contextmanager

0 commit comments

Comments
 (0)