You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
import trio runs cc, ld and objdump on FreeBSD (find_library("pthread") in _thread_cache.py) #3538
On FreeBSD, every import trio starts the C compiler, the linker and objdump. The cause is ctypes.util.find_library("pthread") in src/trio/_core/_thread_cache.py (line 54 on master).
CPython's find_library on FreeBSD first parses ldconfig -r. FreeBSD's threads library is registered there as -lthr.3 (/lib/libthr.so.3), and there's no -lpthread entry, so the lookup misses. It then falls back to _findLib_gcc(), which links a throwaway program with cc -Wl,-t ... -lpthread (this works because /usr/lib/libpthread.so is a symlink to libthr.so), and then runs objdump to read the soname. The answer is right (libthr.so.3), but it costs a toolchain run.
Why it matters: OPNsense (a FreeBSD-based firewall) runs stock Python jobs, one of them every minute, that import dnspython's async resolver, which imports trio. On our firewall, process accounting counted 76 runs each of cc, ld.lld and objdump an hour (about 1,800 a day) on a box that otherwise never uses a toolchain.
Possible fixes, for you to judge:
On FreeBSD, look up thr first (it's found from ldconfig -r alone), or
try ctypes.CDLL(None) first: the process already links the threads library, and pthread_setname_np resolves from it, or
resolve the library lazily, the first time a thread name is set, instead of at import.
Hmm, naively I would expect looking up thr to be bad (option 1), since we in particular want pthreads's interface. I'm not much of a C programmer though, so maybe it's always true that thr satisfies the pthreads interface?
Checking for it in CDLL(None) makes sense to me, it's very cheap and should
work reliably on ELF platforms (so Linux and BSD). Might need something
else for macOS idk, its symbol lookup system is different at least.
ctypes.CDLL(None).pthread_setname_np works at least for my Python installation on MacOS. Using that seems ideal, maybe with a fallback case for the long tail of platforms which might be weird.
Activity
abhishekacharya200 commentedon Oct 12, 2026
Opened #3539, which takes your first suggestion: look up thr before pthread on FreeBSD, keeping the existing fallbacks.
A5rocks commentedon Oct 12, 2026
Hmm, naively I would expect looking up
thrto be bad (option 1), since we in particular wantpthreads's interface. I'm not much of a C programmer though, so maybe it's always true thatthrsatisfies thepthreadsinterface?njsmith commentedon Oct 12, 2026
A5rocks commentedon Oct 12, 2026
ctypes.CDLL(None).pthread_setname_npworks at least for my Python installation on MacOS. Using that seems ideal, maybe with a fallback case for the long tail of platforms which might be weird.njsmith commentedon Oct 12, 2026