Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,25 @@ 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

print(threading.current_thread().native_id, flush=True)
assert threading.current_thread().native_id == threading.get_native_id()
if os.fork() == 0:
print(threading.current_thread().native_id, flush=True)
assert threading.current_thread().native_id == threading.get_native_id()
"""
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):
Expand Down
2 changes: 2 additions & 0 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ def _after_fork(self, new_ident=None):
# This thread is alive.
self._ident = new_ident
assert self._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.
Expand Down
Loading