gh-130115: fix return value of threading.get_ident for the main thread on 32bit musl #130391
+9
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CPython's pthread-based thread identifier relies on pthread_t being able to be represented as an unsigned integer type.
This is true in most Linux libc implementations where it's defined as an unsigned long, however musl typedefs it as a struct *.
If the pointer has the high bit set and is cast to PyThread_ident_t, the resultant value can be sign-extended (https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Arrays-and-pointers-implementation.html). This can cause issues when comparing against threading._MainThread's identifier. The main thread's identifier value is retrieved via _get_main_thread_ident which is backed by an unsigned long which truncates sign extended bits.
Work around this by conditionally compiling in some code for non-glibc based Linux platforms that are at risk of sign-extension to return a PyLong based on the main thread's unsigned long thread identifier if the current thread is the main thread.
musl isn't "officially" supported in PEP 11, however platform detection was added in c163d7f and similar PRs have been merged in the past which target it 5633c4f
This PR is intended to be a "minimum" to get this working. Longer term there should maybe be work to keep
pthread_t
opaque and not make assumptions about its type.I'm open to changing the implementation.
Options I've considered:
modifying
PyThread_get_thread_ident_ex
with a conditional compile directive to cast through eitheruintptr_t
orunsigned long
on potentially affected platforms, which would restore pre GH-110829: Ensure Thread.join() joins the OS thread #110848 behavior for musl. I'm hesitant to bandaid this code, but doing this shouldn't break anything we hadn't made assumptions about prior to the aforementioned PR.Adding a configure check to test if
pthread_t
is arithmetic and then assuming it's a pointer if the check fails. There already exists a similar check forpthread_key_t
for the deprecated (but not removed) TLS API (see PTHREAD_KEY_T_IS_COMPATIBLE_WITH_INT). Howeverpthread_t
could just as easily be a struct in some other libc, though maybe we don't care about this part. Either way, it seemed like too big a change that will hopefully get reverted at some point and i didn't want to risk leaving a configure check that will live forever without re-review (examples: aforementioned TLS API, the getaddrinfo IPv6 check added 20+ years ago, etc)adding a new struct member to
_PyRuntime
that is aPyThread_ident_t
type and updatethread__get_main_thread_ident
to return its value. Adding and maintaining an additional struct member seemed like overkill