Skip to content

Commit 666a484

Browse files
miss-islingtonronaldoussorenvstinner
authored
[3.11] gh-109981: Fix support.fd_count() on macOS 14 (GH-112797) (#112825)
gh-109981: Fix support.fd_count() on macOS 14 (GH-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. (cherry picked from commit 953ee62) Co-authored-by: Ronald Oussoren <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent 5fa2d24 commit 666a484

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
@@ -588,10 +588,17 @@ def fd_count():
588588
"""Count the number of open file descriptors.
589589
"""
590590
if sys.platform.startswith(('linux', 'freebsd', 'emscripten')):
591+
fd_path = "/proc/self/fd"
592+
elif sys.platform == "darwin":
593+
fd_path = "/dev/fd"
594+
else:
595+
fd_path = None
596+
597+
if fd_path is not None:
591598
try:
592-
names = os.listdir("/proc/self/fd")
599+
names = os.listdir(fd_path)
593600
# Subtract one because listdir() internally opens a file
594-
# descriptor to list the content of the /proc/self/fd/ directory.
601+
# descriptor to list the content of the directory.
595602
return len(names) - 1
596603
except FileNotFoundError:
597604
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)