From 77f6c4e81fa5244139cb260c695b96859d0b0dfa Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 22 Nov 2025 17:55:38 +0000 Subject: [PATCH 1/2] flush std streams in the multiprocessing forkserver before fork --- Lib/multiprocessing/forkserver.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/multiprocessing/forkserver.py b/Lib/multiprocessing/forkserver.py index cc8947c5e04fb1..8a4e8d835b0c91 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 From 04f523d58ecf4672a515bff924e8a18fb72279e8 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 22 Nov 2025 18:00:48 +0000 Subject: [PATCH 2/2] NEWS --- .../Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-11-22-18-00-38.gh-issue-98552.d5KNy-.rst 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 00000000000000..37a71ac1fff3f3 --- /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.