From 58badb1711e12b6e8b5240ab96cdd01b95012de7 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" <68491+gpshead@users.noreply.github.com> Date: Sat, 22 Nov 2025 10:29:51 -0800 Subject: [PATCH] gh-98552: flush std streams in the multiprocessing forkserver before fork (#141849) * flush std streams in the multiprocessing forkserver before fork * NEWS --- Lib/multiprocessing/forkserver.py | 1 + .../Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py index cc8947c5e04..8a4e8d835b0 100644 --- a/Lib/multiprocessing/forkserver.py +++ b/Lib/multiprocessing/forkserver.py @@ -326,6 +326,7 @@ def sigchld_handler(*_unused): len(fds))) child_r, child_w, *fds = fds s.close() + util._flush_std_streams() pid = os.fork() if pid == 0: # Child diff --git a/Misc/NEWS.d/next/Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst b/Misc/NEWS.d/next/Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst new file mode 100644 index 00000000000..37a71ac1fff --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst @@ -0,0 +1,4 @@ +The :mod:`multiprocessing` forkserver process now flushes stdout and stderr +before it forks to avoid the confusion children inheriting any buffered but +not yet written output data. Normally there is none, but when using +:func:`multiprocessing.set_forkserver_preload` there *could* be.