Skip to content

Conversation

yuce
Copy link
Contributor

@yuce yuce commented Aug 18, 2025

Fixes: #137884

This PR enables threading.get_native_id on Illumos distributions and Solaris.

Illumos/Solaris defines the thr_self function which returns the thread ID, unique for the process:
https://docs.oracle.com/cd/E36784_01/html/E36874/thr-self-3c.html
On OmniOS and Solaris, thr_self returns the thread IDs from 1, incrementing it for each thread.
Since it is not unique in the system, it is not usable by itself per Python doc:
https://docs.python.org/3/library/threading.html#threading.get_native_id

get_native_id() -> integer
Return a non-negative integer identifying the thread as reported
by the OS (kernel). This may be used to uniquely identify a
particular thread within a system.

In order to make it unique within the system, I put the PID from getpid in the higher 4 bytes, and thread ID from thr_self in the lower 4 bytes of the returned unsigned long.

getpid doesn't return an error value, and it returns pid_t which is 4 bytes (int) on Illumos/Solaris:
https://docs.oracle.com/cd/E23824_01/html/821-1463/getpid-2.html

thr_self doesn't return an error value, and it returns thread_t which is 4 bytes (unsigned int) on Illumos/Solaris:
https://docs.oracle.com/cd/E36784_01/html/E36874/thr-self-3c.html

With the changes in this PR, test_native_id_after_fork in Lib/test/test_threading.py passes, which was skipped before.
This PR does not introduce any new test faliures.

@python-cla-bot
Copy link

python-cla-bot bot commented Aug 18, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 18, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also try extra test for this the change?

yuce and others added 4 commits August 19, 2025 19:23
Co-authored-by: Furkan Onder <furkanonder@protonmail.com>
Co-authored-by: Furkan Onder <furkanonder@protonmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@yuce
Copy link
Contributor Author

yuce commented Aug 19, 2025

@auvipy

shouldn't we also try extra test for this the change?

Could you explain that a bit?
Do you have a specific test in mind?

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@ZeroIntensity
Copy link
Member

shouldn't we also try extra test for this the change?

test_threading already tests for this. The tests for threading.get_native_id are protected by a hasattr, so if a new system gets support for it, that hasattr now passes and runs the test.

Unfortunately, we don't have any working Solaris buildbots to confirm that those tests actually pass now.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) August 20, 2025 16:42
@serhiy-storchaka serhiy-storchaka merged commit 7dc42b6 into python:main Aug 20, 2025
44 checks passed
@serhiy-storchaka
Copy link
Member

I tested on OpenIndiana.

@yuce yuce deleted the fix-issue-137884 branch August 20, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Illumos and Solaris Thread Native ID
6 participants