Skip to content

Commit 9c8f303

Browse files
committed
Enhance comment message, recreate test scenario in _test_multiprocessing
1 parent be1f512 commit 9c8f303

3 files changed

Lines changed: 24 additions & 16 deletions

File tree

Lib/test/_test_multiprocessing.py

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

6847+
@support.requires_fork()
6848+
def test_forked_not_started(self):
6849+
# gh-134381
6850+
# Ensure that a forked parent process, which has not been started yet,
6851+
# can be initiated within the child process.
6852+
6853+
ctx = multiprocessing.get_context("fork") # local “fork” cont
6854+
q = ctx.Queue()
6855+
t = threading.Thread(target=lambda: q.put("done"), daemon=True)
6856+
6857+
def child():
6858+
t.start()
6859+
t.join()
6860+
6861+
p = ctx.Process(target=child)
6862+
p.start()
6863+
p.join(support.SHORT_TIMEOUT)
6864+
6865+
self.assertEqual(p.exitcode, 0)
6866+
self.assertEqual(q.get_nowait(), "done")
6867+
close_queue(q)
6868+
68476869

68486870
#
68496871
# Mixins

Lib/test/test_thread.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,6 @@ def fork_thread(read_fd, write_fd):
439439
self.assertIsNotNone(pid)
440440
support.wait_process(pid, exitcode=0)
441441

442-
@support.requires_fork()
443-
@threading_helper.reap_threads
444-
def test_forked_not_started(self):
445-
handle = thread._ThreadHandle()
446-
pid = os.fork()
447-
if pid == 0:
448-
# child process
449-
try:
450-
self.assertTrue(handle.is_alive())
451-
self.assertFalse(handle.is_done())
452-
finally:
453-
os._exit(0)
454-
else:
455-
support.wait_process(pid, exitcode=0)
456-
457442
def tearDown(self):
458443
try:
459444
os.close(self.read_fd)

Modules/_threadmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ _PyThread_AfterFork(struct _pythread_runtime_state *state)
296296
continue;
297297
}
298298

299-
// keep not_start handles – they are safe to start in the child
299+
// Keep handles for threads that have not been started yet. They are
300+
// safe to start in the child process.
300301
if (handle->state == THREAD_HANDLE_NOT_STARTED){
301302
continue;
302303
}

0 commit comments

Comments
 (0)