Skip to content

Commit 19b7d6c

Browse files
committed
Add test for native thread ID after fork
1 parent 3793526 commit 19b7d6c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Lib/test/test_threading.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,25 @@ def do_flush(*args, **kwargs):
12761276
''')
12771277
assert_python_ok("-c", script)
12781278

1279+
@skip_unless_reliable_fork
1280+
def test_native_id_after_fork(self):
1281+
script = """if True:
1282+
import threading
1283+
import os
1284+
1285+
print(threading.current_thread().native_id, flush=True)
1286+
assert threading.current_thread().native_id == threading.get_native_id()
1287+
if os.fork() == 0:
1288+
print(threading.current_thread().native_id, flush=True)
1289+
assert threading.current_thread().native_id == threading.get_native_id()
1290+
"""
1291+
rc, out, err = assert_python_ok('-c', script)
1292+
self.assertEqual(rc, 0)
1293+
self.assertEqual(err, b"")
1294+
native_ids = out.strip().splitlines()
1295+
self.assertEqual(len(native_ids), 2)
1296+
self.assertNotEqual(native_ids[0], native_ids[1])
1297+
12791298
class ThreadJoinOnShutdown(BaseTestCase):
12801299

12811300
def _run_and_join(self, script):

Lib/threading.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,8 @@ def _after_fork(self, new_ident=None):
951951
# This thread is alive.
952952
self._ident = new_ident
953953
assert self._handle.ident == new_ident
954+
if _HAVE_THREAD_NATIVE_ID:
955+
self._set_native_id()
954956
else:
955957
# Otherwise, the thread is dead, Jim. _PyThread_AfterFork()
956958
# already marked our handle done.

0 commit comments

Comments
 (0)