Skip to content

Commit 953ee62

Browse files
gh-109981: Fix support.fd_count() on macOS 14 (#112797)
Use scanning "/dev/fd/" on macOS in support.fd_count(). That's both more efficient than scanning all possible file descriptors, and avoids crashing the interpreter when there are open "guarded" file descriptors. "Guarded" file descriptors are a macOS feature where file descriptors used by system libraries are marked and cause hard crashes when used by "user" code. Co-authored-by: Victor Stinner <[email protected]>
1 parent 16448ca commit 953ee62

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Lib/test/support/os_helper.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,17 @@ def fd_count():
592592
"""Count the number of open file descriptors.
593593
"""
594594
if sys.platform.startswith(('linux', 'freebsd', 'emscripten')):
595+
fd_path = "/proc/self/fd"
596+
elif sys.platform == "darwin":
597+
fd_path = "/dev/fd"
598+
else:
599+
fd_path = None
600+
601+
if fd_path is not None:
595602
try:
596-
names = os.listdir("/proc/self/fd")
603+
names = os.listdir(fd_path)
597604
# Subtract one because listdir() internally opens a file
598-
# descriptor to list the content of the /proc/self/fd/ directory.
605+
# descriptor to list the content of the directory.
599606
return len(names) - 1
600607
except FileNotFoundError:
601608
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Use ``/dev/fd`` on macOS to determine the number of open files in
2+
``test.support.os_helper.fd_count`` to avoid a crash with "guarded" file
3+
descriptors when probing for open files.

0 commit comments

Comments
 (0)