Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2939.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The pthread functions are now correctly found on systems using vanilla versions of musl libc.
9 changes: 7 additions & 2 deletions src/trio/_core/_thread_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions src/trio/_tests/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down