diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 96b43936be92cd..d1e88b91646066 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -1580,6 +1580,14 @@ def test_3_join_in_forked_from_thread(self): main_thread = threading.current_thread() def worker(): + # fork() must happen before the interpreter starts shutting + # down: otherwise the main thread is already marked as + # stopped in the parent, the child inherits that state, and + # the test below succeeds without testing anything. + if not main_thread.is_alive(): + print('main thread stopped before fork()', + file=sys.stderr, flush=True) + os._exit(3) childpid = os.fork() if childpid != 0: # parent process @@ -1595,6 +1603,9 @@ def worker(): w = threading.Thread(target=worker) w.start() + # Keep the main thread alive until worker() has forked, so that + # the check above cannot be lost to interpreter shutdown. + w.join() """ self._run_and_join(script) diff --git a/Misc/NEWS.d/next/Tests/2026-09-28-12-05-10.gh-issue-116612.Rn4kLp.rst b/Misc/NEWS.d/next/Tests/2026-09-28-12-05-10.gh-issue-116612.Rn4kLp.rst new file mode 100644 index 00000000000000..650c51d19f96aa --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-09-28-12-05-10.gh-issue-116612.Rn4kLp.rst @@ -0,0 +1,3 @@ +Make ``test_3_join_in_forked_from_thread`` in ``test_threading`` wait for the +forking thread, so that the fork happens while the main thread is still +running rather than racing interpreter shutdown.