https://github.com/python/cpython/commit/8e8d5c91cd9e161a74463ebbee7a1a563d12b637
commit: 8e8d5c91cd9e161a74463ebbee7a1a563d12b637
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-05-20T16:46:52Z
summary:

[3.14] gh-132542: Set native thread ID after fork (GH-132701) (GH-134356)

(cherry picked from commit 6b735023132a4ac9dc5b849d982104eeb1e8bdad)

Co-authored-by: Noam Cohen <n...@noam.me>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-17-16-46.gh-issue-132542.7T_TY_.rst
M Lib/test/test_threading.py
M Lib/threading.py

diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index abe63c10c0ac7c..dc7172db04c5b7 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -1352,6 +1352,34 @@ def do_flush(*args, **kwargs):
         ''')
         assert_python_ok("-c", script)
 
+    @skip_unless_reliable_fork
+    def test_native_id_after_fork(self):
+        script = """if True:
+            import threading
+            import os
+            from test import support
+
+            parent_thread_native_id = threading.current_thread().native_id
+            print(parent_thread_native_id, flush=True)
+            assert parent_thread_native_id == threading.get_native_id()
+            childpid = os.fork()
+            if childpid == 0:
+                print(threading.current_thread().native_id, flush=True)
+                assert threading.current_thread().native_id == 
threading.get_native_id()
+            else:
+                try:
+                    assert parent_thread_native_id == 
threading.current_thread().native_id
+                    assert parent_thread_native_id == threading.get_native_id()
+                finally:
+                    support.wait_process(childpid, exitcode=0)
+            """
+        rc, out, err = assert_python_ok('-c', script)
+        self.assertEqual(rc, 0)
+        self.assertEqual(err, b"")
+        native_ids = out.strip().splitlines()
+        self.assertEqual(len(native_ids), 2)
+        self.assertNotEqual(native_ids[0], native_ids[1])
+
 class ThreadJoinOnShutdown(BaseTestCase):
 
     def _run_and_join(self, script):
diff --git a/Lib/threading.py b/Lib/threading.py
index 39a1a7f4cdfda0..fa290d17c635ab 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -951,6 +951,8 @@ def _after_fork(self, new_ident=None):
             # This thread is alive.
             self._ident = new_ident
             assert self._os_thread_handle.ident == new_ident
+            if _HAVE_THREAD_NATIVE_ID:
+                self._set_native_id()
         else:
             # Otherwise, the thread is dead, Jim.  _PyThread_AfterFork()
             # already marked our handle done.
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-17-16-46.gh-issue-132542.7T_TY_.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-17-16-46.gh-issue-132542.7T_TY_.rst
new file mode 100644
index 00000000000000..c69ce5efdedcca
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-19-17-16-46.gh-issue-132542.7T_TY_.rst
@@ -0,0 +1,2 @@
+Update :attr:`Thread.native_id <threading.Thread.native_id>` after
+:manpage:`fork(2)` to ensure accuracy. Patch by Noam Cohen.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to