Skip to content

Commit

Permalink
[3.11] pythongh-109981: Fix support.fd_count() on macOS 14 (pythonGH-…
Browse files Browse the repository at this point in the history
…112797) (python#112825)

pythongh-109981: Fix support.fd_count() on macOS 14 (pythonGH-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]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent 5fa2d24 commit 666a484
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,17 @@ def fd_count():
"""Count the number of open file descriptors.
"""
if sys.platform.startswith(('linux', 'freebsd', 'emscripten')):
fd_path = "/proc/self/fd"
elif sys.platform == "darwin":
fd_path = "/dev/fd"
else:
fd_path = None

if fd_path is not None:
try:
names = os.listdir("/proc/self/fd")
names = os.listdir(fd_path)
# Subtract one because listdir() internally opens a file
# descriptor to list the content of the /proc/self/fd/ directory.
# descriptor to list the content of the directory.
return len(names) - 1
except FileNotFoundError:
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use ``/dev/fd`` on macOS to determine the number of open files in
``test.support.os_helper.fd_count`` to avoid a crash with "guarded" file
descriptors when probing for open files.

0 comments on commit 666a484

Please sign in to comment.