File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed
Misc/NEWS.d/next/Core_and_Builtins Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ Fix :exc: `RuntimeError ` when using a not-started :class: `threading.Thread ` after calling :func: `os.fork `
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments