Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aebbbaf

Browse filesBrowse files
authored
[3.13] gh-132542: Set native thread ID after fork (GH-132701) (GH-134361)
(cherry picked from commit 6b73502)
1 parent bad9f63 commit aebbbaf
Copy full SHA for aebbbaf

File tree

Expand file treeCollapse file tree

3 files changed

+32
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+32
-0
lines changed

‎Lib/test/test_threading.py

Copy file name to clipboardExpand all lines: Lib/test/test_threading.py
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,34 @@ def f():
12081208
self.assertEqual(err, b'')
12091209

12101210

1211+
@skip_unless_reliable_fork
1212+
def test_native_id_after_fork(self):
1213+
script = """if True:
1214+
import threading
1215+
import os
1216+
from test import support
1217+
1218+
parent_thread_native_id = threading.current_thread().native_id
1219+
print(parent_thread_native_id, flush=True)
1220+
assert parent_thread_native_id == threading.get_native_id()
1221+
childpid = os.fork()
1222+
if childpid == 0:
1223+
print(threading.current_thread().native_id, flush=True)
1224+
assert threading.current_thread().native_id == threading.get_native_id()
1225+
else:
1226+
try:
1227+
assert parent_thread_native_id == threading.current_thread().native_id
1228+
assert parent_thread_native_id == threading.get_native_id()
1229+
finally:
1230+
support.wait_process(childpid, exitcode=0)
1231+
"""
1232+
rc, out, err = assert_python_ok('-c', script)
1233+
self.assertEqual(rc, 0)
1234+
self.assertEqual(err, b"")
1235+
native_ids = out.strip().splitlines()
1236+
self.assertEqual(len(native_ids), 2)
1237+
self.assertNotEqual(native_ids[0], native_ids[1])
1238+
12111239
class ThreadJoinOnShutdown(BaseTestCase):
12121240

12131241
def _run_and_join(self, script):

‎Lib/threading.py

Copy file name to clipboardExpand all lines: Lib/threading.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,8 @@ def _after_fork(self, new_ident=None):
932932
# This thread is alive.
933933
self._ident = new_ident
934934
assert self._handle.ident == new_ident
935+
if _HAVE_THREAD_NATIVE_ID:
936+
self._set_native_id()
935937
else:
936938
# Otherwise, the thread is dead, Jim. _PyThread_AfterFork()
937939
# already marked our handle done.
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :attr:`Thread.native_id <threading.Thread.native_id>` after
2+
:manpage:`fork(2)` to ensure accuracy. Patch by Noam Cohen.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.