python-trio / trio

Trio – a friendly Python library for async concurrency and I/O
https://trio.readthedocs.io
Other
5.98k stars 325 forks source link

Fix finding pthread_*name_np on vanilla musl libc #2939

Closed mgorny closed 5 months ago

mgorny commented 5 months ago

Fix the pthread_getname_np and pthread_setname_np search logic to support vanilla versions of musl and CPython, e.g. as used on Gentoo musl systems. On such systems, there is no "libpthread.so" (there is only a static library) and the relevant functions are found in "libc.so". Additionally, ctypes.util.find_library("c") does not work because of an old unsolved bug in CPython (linked in the code).

To resolve the problem, add a fallback to trying libc.so if no pthread library can be found. This roughly covers three possibilities:

The code in get_os_thread_name_func() remains fully relaxed, allowing either CDLL construction (i.e. finding the library) to fail, or the library not to contain pthread_setname_np.

The code in test_threads.py was made more relaxed — rather than skipping if libpthread.so does not exist, it tries to load libc.so as a fallback, and skips if that fails.

Originally reported as https://bugs.gentoo.org/923257.

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (05c5df2) 99.64% compared to head (c8c1957) 99.64%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #2939 +/- ## ======================================= Coverage 99.64% 99.64% ======================================= Files 116 116 Lines 17503 17506 +3 Branches 3148 3148 ======================================= + Hits 17441 17444 +3 Misses 43 43 Partials 19 19 ``` | [Files](https://app.codecov.io/gh/python-trio/trio/pull/2939?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio) | Coverage Δ | | |---|---|---| | [src/trio/\_core/\_thread\_cache.py](https://app.codecov.io/gh/python-trio/trio/pull/2939?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX2NvcmUvX3RocmVhZF9jYWNoZS5weQ==) | `100.00% <100.00%> (ø)` | | | [src/trio/\_tests/test\_threads.py](https://app.codecov.io/gh/python-trio/trio/pull/2939?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python-trio#diff-c3JjL3RyaW8vX3Rlc3RzL3Rlc3RfdGhyZWFkcy5weQ==) | `100.00% <100.00%> (ø)` | |
A5rocks commented 5 months ago

I ran into this for https://github.com/python-trio/trio/pull/2933 and ended up just making that require musl-dev instead of figuring this out. Thanks for putting in the work!

mgorny commented 5 months ago

Fixed and added a newsfragment.

trio-bot[bot] commented 5 months ago

Hey @mgorny, it looks like that was the first time we merged one of your PRs! Thanks so much! :tada: :birthday:

If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:

If you want to read more, here's the relevant section in our contributing guide.

Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you.

If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out!

mgorny commented 5 months ago

Thanks!

jakkdl commented 5 months ago

Thank you for the contribution! Can also see that it works in CI over in #2933 https://github.com/python-trio/trio/actions/runs/7712303460/job/21019556518?pr=2933