-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
threading.Thread.native_id for forking thread wrong after fork #132542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
serhiy-storchaka
pushed a commit
that referenced
this issue
May 20, 2025
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
May 20, 2025
(cherry picked from commit 6b73502) Co-authored-by: Noam Cohen <[email protected]>
This was referenced May 20, 2025
serhiy-storchaka
pushed a commit
that referenced
this issue
May 20, 2025
) (cherry picked from commit 6b73502) Co-authored-by: Noam Cohen <[email protected]>
serhiy-storchaka
pushed a commit
that referenced
this issue
May 20, 2025
serhiy-storchaka
pushed a commit
that referenced
this issue
May 21, 2025
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
May 21, 2025
…upported (pythonGH-134408) (cherry picked from commit e6dde10) Co-authored-by: Noam Cohen <[email protected]>
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
May 21, 2025
…upported (pythonGH-134408) (cherry picked from commit e6dde10) Co-authored-by: Noam Cohen <[email protected]>
serhiy-storchaka
pushed a commit
that referenced
this issue
May 21, 2025
…supported (GH-134408) (GH-134414) (cherry picked from commit e6dde10) Co-authored-by: Noam Cohen <[email protected]>
serhiy-storchaka
pushed a commit
that referenced
this issue
May 21, 2025
…supported (GH-134408) (GH-134413) (cherry picked from commit e6dde10) Co-authored-by: Noam Cohen <[email protected]>
lkollar
pushed a commit
to lkollar/cpython
that referenced
this issue
May 26, 2025
lkollar
pushed a commit
to lkollar/cpython
that referenced
this issue
May 26, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
On Linux, native thread IDs are unique across processes. This means that the native thread ID of a forking thread necessarily changes at forking. The threading module initializes the thread ID only when the thread is started. Hence, after forking, the old thread ID is retained, i.e. it becomes wrong for the forked process.
This prints for example:
Notice that the child (c) still prints the same native thread ID 227148 as the the parent while in fact the thread ID has changed to 227150 and the old native thread ID is not present at all in the child process.
This is not specific to the main thread though, as the following variant of the sample demonstrates:
This multi-threaded sample prints for example:
This bug was previously seen in #82888 with the multiprocessing module but only worked around in #17088 for the multiprocessing module in and it is still present when using any other way of forking.
CPython versions tested on:
3.13, 3.8
Operating systems tested on:
Linux
Proposed Solution
This could be solved with an atfork_child handler if you don't care about the ID being wrong in earlier atfork_child handlers. Safer but more complex would be marking the thread as being in-forking in atfork-prepare and then re-query the native thread IDevery time until atfork-child/parent unmarks the thread.
The native thread ID in the C API Thread state might also require a fix, I did not check it.
Linked PRs
test_native_id_after_fork
ifnative_id
is supported #134408The text was updated successfully, but these errors were encountered: