Skip to content

Commit e4c4ecc

Browse files
[3.13] gh-134381: Fix RuntimeError when starting not-yet started Thread after fork (gh-134514) (gh-134597)
(cherry picked from commit 9a2346d) Co-authored-by: Jiucheng(Oliver) <[email protected]>
1 parent 0308612 commit e4c4ecc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6494,6 +6494,28 @@ def f(x): return x*x
64946494
self.assertEqual("332833500", out.decode('utf-8').strip())
64956495
self.assertFalse(err, msg=err.decode('utf-8'))
64966496

6497+
def test_forked_thread_not_started(self):
6498+
# gh-134381: Ensure that a thread that has not been started yet in
6499+
# the parent process can be started within a forked child process.
6500+
6501+
if multiprocessing.get_start_method() != "fork":
6502+
self.skipTest("fork specific test")
6503+
6504+
q = multiprocessing.Queue()
6505+
t = threading.Thread(target=lambda: q.put("done"), daemon=True)
6506+
6507+
def child():
6508+
t.start()
6509+
t.join()
6510+
6511+
p = multiprocessing.Process(target=child)
6512+
p.start()
6513+
p.join(support.SHORT_TIMEOUT)
6514+
6515+
self.assertEqual(p.exitcode, 0)
6516+
self.assertEqual(q.get_nowait(), "done")
6517+
close_queue(q)
6518+
64976519

64986520
#
64996521
# Mixins
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :exc:`RuntimeError` when using a not-started :class:`threading.Thread` after calling :func:`os.fork`

Modules/_threadmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
262262
continue;
263263
}
264264

265+
// Keep handles for threads that have not been started yet. They are
266+
// safe to start in the child process.
267+
if (handle->state == THREAD_HANDLE_NOT_STARTED) {
268+
continue;
269+
}
270+
265271
// Mark all threads as done. Any attempts to join or detach the
266272
// underlying OS thread (if any) could crash. We are the only thread;
267273
// it's safe to set this non-atomically.

0 commit comments

Comments
 (0)