diff --git a/newsfragments/2939.bugfix.rst b/newsfragments/2939.bugfix.rst new file mode 100644 index 0000000000..7ce40e6933 --- /dev/null +++ b/newsfragments/2939.bugfix.rst @@ -0,0 +1 @@ +The pthread functions are now correctly found on systems using vanilla versions of musl libc. diff --git a/src/trio/_core/_thread_cache.py b/src/trio/_core/_thread_cache.py index 60279c87e2..8144182121 100644 --- a/src/trio/_core/_thread_cache.py +++ b/src/trio/_core/_thread_cache.py @@ -41,10 +41,15 @@ def darwin_namefunc( setname(_to_os_thread_name(name)) # find the pthread library - # this will fail on windows + # this will fail on windows and musl libpthread_path = ctypes.util.find_library("pthread") if not libpthread_path: - return None + # musl includes pthread functions directly in libc.so + # (but note that find_library("c") does not work on musl, + # see: https://github.com/python/cpython/issues/65821) + # so try that library instead + # if it doesn't exist, CDLL() will fail below + libpthread_path = "libc.so" # Sometimes windows can find the path, but gives a permission error when # accessing it. Catching a wider exception in case of more esoteric errors. diff --git a/src/trio/_tests/test_threads.py b/src/trio/_tests/test_threads.py index aefb4ba27b..326cffd6b7 100644 --- a/src/trio/_tests/test_threads.py +++ b/src/trio/_tests/test_threads.py @@ -237,9 +237,17 @@ def _get_thread_name(ident: int | None = None) -> str | None: libpthread_path = ctypes.util.find_library("pthread") if not libpthread_path: - print(f"no pthread on {sys.platform})") + # musl includes pthread functions directly in libc.so + # (but note that find_library("c") does not work on musl, + # see: https://github.com/python/cpython/issues/65821) + # so try that library instead + # if it doesn't exist, CDLL() will fail below + libpthread_path = "libc.so" + try: + libpthread = ctypes.CDLL(libpthread_path) + except Exception: + print(f"no pthread on {sys.platform}") return None - libpthread = ctypes.CDLL(libpthread_path) pthread_getname_np = getattr(libpthread, "pthread_getname_np", None)