File tree 3 files changed +29
-0
lines changed
Misc/NEWS.d/next/Core_and_Builtins 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
6494
6494
self .assertEqual ("332833500" , out .decode ('utf-8' ).strip ())
6495
6495
self .assertFalse (err , msg = err .decode ('utf-8' ))
6496
6496
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
+
6497
6519
6498
6520
#
6499
6521
# 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)
262
262
continue ;
263
263
}
264
264
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
+
265
271
// Mark all threads as done. Any attempts to join or detach the
266
272
// underlying OS thread (if any) could crash. We are the only thread;
267
273
// it's safe to set this non-atomically.
You can’t perform that action at this time.
0 commit comments