From 24746fb4046baa6d216fed636fa8b783bfb23715 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Wed, 6 Dec 2023 12:11:22 +0100 Subject: [PATCH 1/3] gh-109981: test crash on macOS 14 This fixes a crash on macOS 14 when running ``./python.exe -m test test_sax test_socket test_support``. The root cause of this is a macOS feature where file descriptors used by system libraries are guarded an will cause a hard crash when used in "user" code. --- Lib/test/support/os_helper.py | 9 +++++++++ .../macOS/2023-12-06-12-11-13.gh-issue-109981.mOHg10.rst | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/macOS/2023-12-06-12-11-13.gh-issue-109981.mOHg10.rst diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py index 46ae53aa11a91f..5ee241b4acf1c6 100644 --- a/Lib/test/support/os_helper.py +++ b/Lib/test/support/os_helper.py @@ -600,6 +600,15 @@ def fd_count(): except FileNotFoundError: pass + if sys.platform == 'darwin': + try: + names = os.listdir("/dev/fd") + # Subtract one because listdir() internally opens a file + # descriptor to list the content of the /dev/fd directory. + return len(names) - 1 + except FileNotFoundError: + pass + MAXFD = 256 if hasattr(os, 'sysconf'): try: diff --git a/Misc/NEWS.d/next/macOS/2023-12-06-12-11-13.gh-issue-109981.mOHg10.rst b/Misc/NEWS.d/next/macOS/2023-12-06-12-11-13.gh-issue-109981.mOHg10.rst new file mode 100644 index 00000000000000..f86ab2c37ee6ec --- /dev/null +++ b/Misc/NEWS.d/next/macOS/2023-12-06-12-11-13.gh-issue-109981.mOHg10.rst @@ -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. From 673672f4d4a4f2345b3f7ac4e662bbe031f44e75 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Wed, 6 Dec 2023 12:23:22 +0100 Subject: [PATCH 2/3] Remove unnecessary code duplication --- Lib/test/support/os_helper.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py index 5ee241b4acf1c6..05251ffcfdc675 100644 --- a/Lib/test/support/os_helper.py +++ b/Lib/test/support/os_helper.py @@ -591,20 +591,17 @@ def __fspath__(self): def fd_count(): """Count the number of open file descriptors. """ + fd_path = None if sys.platform.startswith(('linux', 'freebsd', 'emscripten')): - try: - names = os.listdir("/proc/self/fd") - # Subtract one because listdir() internally opens a file - # descriptor to list the content of the /proc/self/fd/ directory. - return len(names) - 1 - except FileNotFoundError: - pass + fd_path = "/proc/self/fd" + elif sys.platform == "darwin": + fd_path = "/dev/fd" - if sys.platform == 'darwin': + if fd_path is not None: try: - names = os.listdir("/dev/fd") + names = os.listdir(fd_path) # Subtract one because listdir() internally opens a file - # descriptor to list the content of the /dev/fd directory. + # descriptor to list the content of the directory. return len(names) - 1 except FileNotFoundError: pass From bb098610f3fb1f2568b457227289f452c3997076 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Thu, 7 Dec 2023 09:37:19 +0100 Subject: [PATCH 3/3] Update Lib/test/support/os_helper.py Co-authored-by: Victor Stinner --- Lib/test/support/os_helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py index 05251ffcfdc675..7a67d87fb9e846 100644 --- a/Lib/test/support/os_helper.py +++ b/Lib/test/support/os_helper.py @@ -591,11 +591,12 @@ def __fspath__(self): def fd_count(): """Count the number of open file descriptors. """ - fd_path = None 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: